feat: add local api for versions on globals

This commit is contained in:
Dan Ribbens
2022-02-14 10:53:16 -05:00
parent 85221e6109
commit 4072e7ee06
5 changed files with 158 additions and 19 deletions

View File

@@ -1,23 +1,23 @@
import { Document, Where } from 'payload/types';
import { Response } from 'express';
import { Document, Where } from '../../../types';
import { SanitizedGlobalConfig } from '../../config/types';
import { PayloadRequest } from '../../../express/types';
// export type Resolver = (
// _: unknown,
// args: {
// locale?: string
// fallbackLocale?: string
// where: Where
// limit?: number
// page?: number
// sort?: string
// },
// context: {
// req: PayloadRequest,
// res: Response
// }
// ) => Promise<Document>
export type Resolver = (
_: unknown,
args: {
locale?: string
fallbackLocale?: string
where: Where
limit?: number
page?: number
sort?: string
},
context: {
req: PayloadRequest,
res: Response
}
) => Promise<Document>
function findVersions(globalConfig: SanitizedGlobalConfig) {
async function resolver(_, args, context) {

View File

@@ -0,0 +1,49 @@
import { Document } from '../../../types';
import { TypeWithVersion } from '../../../versions/types';
export type Options = {
slug: string
id: string
depth?: number
locale?: string
fallbackLocale?: string
user?: Document
overrideAccess?: boolean
showHiddenFields?: boolean
disableErrors?: boolean
}
async function findVersionByID<T extends TypeWithVersion<T> = any>(options: Options): Promise<Document> {
const {
slug: globalSlug,
depth,
id,
locale = this?.config?.localization?.defaultLocale,
fallbackLocale = null,
user,
overrideAccess = true,
disableErrors = false,
showHiddenFields,
} = options;
const globalConfig = this.globals.config.find((config) => config.slug === globalSlug);
return this.operations.globals.findVersionByID({
slug: globalSlug,
depth,
id,
globalConfig,
overrideAccess,
disableErrors,
showHiddenFields,
req: {
user,
payloadAPI: 'local',
locale,
fallbackLocale,
payload: this,
},
});
}
export default findVersionByID;

View File

@@ -0,0 +1,56 @@
import { Document, Where } from '../../../types';
import { PaginatedDocs } from '../../../mongoose/types';
import { TypeWithVersion } from '../../../versions/types';
export type Options = {
slug: string
depth?: number
page?: number
limit?: number
locale?: string
fallbackLocale?: string
user?: Document
overrideAccess?: boolean
showHiddenFields?: boolean
disableErrors?: boolean
sort?: string
where?: Where
}
export default async function findVersions<T extends TypeWithVersion<T> = any>(options: Options): Promise<PaginatedDocs<T>> {
const {
slug: globalSlug,
depth,
page,
limit,
where,
locale = this?.config?.localization?.defaultLocale,
fallbackLocale = null,
user,
overrideAccess = true,
showHiddenFields,
disableErrors = false,
sort,
} = options;
const globalConfig = this.globals.config.find((config) => config.slug === globalSlug);
return this.operations.globals.findVersions({
where,
page,
limit,
depth,
globalConfig,
sort,
overrideAccess,
showHiddenFields,
disableErrors,
req: {
user,
payloadAPI: 'local',
locale,
fallbackLocale,
payload: this,
},
});
}

View File

@@ -0,0 +1,37 @@
import { Document } from '../../../types';
import { TypeWithVersion } from '../../../versions/types';
export type Options = {
slug: string
id: string
depth?: number
user?: Document
overrideAccess?: boolean
showHiddenFields?: boolean
}
export default async function restoreVersion<T extends TypeWithVersion<T> = any>(options: Options): Promise<T> {
const {
slug: globalSlug,
depth,
id,
user,
overrideAccess = true,
showHiddenFields,
} = options;
const globalConfig = this.globals[globalSlug];
return this.operations.globals.restoreVersion({
depth,
globalConfig,
overrideAccess,
id,
showHiddenFields,
req: {
user,
payloadAPI: 'local',
payload: this,
},
});
}

View File

@@ -10,9 +10,6 @@ import { NotFound } from '../../errors';
export type Arguments = {
globalConfig: SanitizedGlobalConfig
id: string
page?: number
limit?: number
sort?: string
depth?: number
req?: PayloadRequest
overrideAccess?: boolean