@payloadcms/figma 0.0.1-alpha.5 → 0.0.1-alpha.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/control-plane.d.ts +31 -1
- package/dist/api/control-plane.js +137 -67
- package/dist/api/figma-api.d.ts +20 -0
- package/dist/api/figma-api.js +77 -21
- package/dist/auth/callback-server.js +2 -1
- package/dist/auth/crypto-utils.js +5 -5
- package/dist/auth/jwt-validator.d.ts +2 -1
- package/dist/auth/jwt-validator.js +2 -2
- package/dist/auth/oauth-flow.d.ts +1 -1
- package/dist/auth/oauth-flow.js +15 -11
- package/dist/auth/project-token.js +11 -6
- package/dist/auth/refresh.d.ts +2 -1
- package/dist/auth/refresh.js +25 -12
- package/dist/auth/token-store.d.ts +40 -1
- package/dist/auth/token-store.js +65 -5
- package/dist/auth/types.d.ts +17 -4
- package/dist/cli.js +54 -6
- package/dist/commands/build-lambda-zip.d.ts +5 -0
- package/dist/commands/build-lambda-zip.js +19 -0
- package/dist/commands/debug.d.ts +8 -0
- package/dist/commands/debug.js +161 -0
- package/dist/commands/deploy.d.ts +6 -0
- package/dist/commands/deploy.js +94 -126
- package/dist/commands/env.d.ts +13 -0
- package/dist/commands/env.js +88 -0
- package/dist/commands/init.d.ts +3 -5
- package/dist/commands/init.js +118 -94
- package/dist/commands/list-tokens.d.ts +3 -3
- package/dist/commands/list-tokens.js +22 -7
- package/dist/commands/login.d.ts +8 -1
- package/dist/commands/login.js +30 -12
- package/dist/commands/logout.d.ts +5 -4
- package/dist/commands/logout.js +56 -13
- package/dist/config/oauth.d.ts +4 -14
- package/dist/config/oauth.js +18 -40
- package/dist/constants.d.ts +36 -19
- package/dist/constants.js +58 -20
- package/dist/db-content-api/README.md +98 -0
- package/dist/db-content-api/generated/content-api-types.d.ts +3746 -0
- package/dist/db-content-api/generated/content-api-types.js +7 -0
- package/dist/db-content-api/index.d.ts +20 -0
- package/dist/db-content-api/index.js +702 -0
- package/dist/db-content-api/temp-utilities/slug.d.ts +2 -0
- package/dist/db-content-api/temp-utilities/slug.js +3 -0
- package/dist/db-content-api/temp-utilities/sorting.d.ts +3 -0
- package/dist/db-content-api/temp-utilities/sorting.js +32 -0
- package/dist/db-content-api/temp-utilities/types.d.ts +22 -0
- package/dist/db-content-api/temp-utilities/types.js +15 -0
- package/dist/db-content-api/temp-utilities/unwrapDocument.d.ts +34 -0
- package/dist/db-content-api/temp-utilities/unwrapDocument.js +53 -0
- package/dist/db-content-api/utilities/auth.d.ts +30 -0
- package/dist/db-content-api/utilities/auth.js +81 -0
- package/dist/db-content-api/utilities/data/applyDefaults.d.ts +7 -0
- package/dist/db-content-api/utilities/data/applyDefaults.js +59 -0
- package/dist/db-content-api/utilities/data/castFieldValue.d.ts +11 -0
- package/dist/db-content-api/utilities/data/castFieldValue.js +69 -0
- package/dist/db-content-api/utilities/data/index.d.ts +42 -0
- package/dist/db-content-api/utilities/data/index.js +211 -0
- package/dist/db-content-api/utilities/data/removeVirtualFields.d.ts +12 -0
- package/dist/db-content-api/utilities/data/removeVirtualFields.js +54 -0
- package/dist/db-content-api/utilities/data/stripFields.d.ts +48 -0
- package/dist/db-content-api/utilities/data/stripFields.js +195 -0
- package/dist/db-content-api/utilities/data/transformPublishedLocale.d.ts +23 -0
- package/dist/db-content-api/utilities/data/transformPublishedLocale.js +42 -0
- package/dist/db-content-api/utilities/joins.d.ts +55 -0
- package/dist/db-content-api/utilities/joins.js +136 -0
- package/dist/db-content-api/utilities/locale/index.d.ts +10 -0
- package/dist/db-content-api/utilities/locale/index.js +21 -0
- package/dist/db-content-api/utilities/meta/buildLocalizedPaths.d.ts +11 -0
- package/dist/db-content-api/utilities/meta/buildLocalizedPaths.js +66 -0
- package/dist/db-content-api/utilities/meta/buildMeta.d.ts +37 -0
- package/dist/db-content-api/utilities/meta/buildMeta.js +33 -0
- package/dist/db-content-api/utilities/meta/buildPathTypes.d.ts +13 -0
- package/dist/db-content-api/utilities/meta/buildPathTypes.js +181 -0
- package/dist/db-content-api/utilities/where.d.ts +20 -0
- package/dist/db-content-api/utilities/where.js +127 -0
- package/dist/endpoints/health.d.ts +3 -0
- package/dist/endpoints/health.js +11 -0
- package/dist/endpoints/schema.d.ts +3 -0
- package/dist/endpoints/schema.js +29 -0
- package/dist/exports/client.d.ts +3 -0
- package/dist/exports/client.js +3 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3 -1
- package/dist/oauth/components/FigmaAvatar/index.d.ts +4 -0
- package/dist/oauth/components/FigmaAvatar/index.js +27 -0
- package/dist/oauth/components/FigmaAvatar/index.scss +12 -0
- package/dist/oauth/components/FigmaAvatarCell/index.d.ts +5 -0
- package/dist/oauth/components/FigmaAvatarCell/index.js +25 -0
- package/dist/oauth/components/FigmaAvatarCell/index.scss +5 -0
- package/dist/oauth/components/FigmaAvatarPlaceholder/index.d.ts +9 -0
- package/dist/oauth/components/FigmaAvatarPlaceholder/index.js +37 -0
- package/dist/oauth/components/FigmaAvatarPlaceholder/index.scss +28 -0
- package/dist/oauth/components/LoginButton/index.js +50 -6
- package/dist/oauth/components/LoginButton/index.scss +50 -3
- package/dist/oauth/components/LogoutButton/index.js +5 -4
- package/dist/oauth/defaults.js +67 -10
- package/dist/oauth/endpoints/getLoginEndpoint.js +69 -24
- package/dist/oauth/endpoints/getLogoutEndpoint.js +7 -4
- package/dist/oauth/index.js +40 -5
- package/dist/oauth/strategy/index.d.ts +0 -2
- package/dist/oauth/strategy/index.js +40 -17
- package/dist/oauth/types.d.ts +6 -6
- package/dist/oauth/utilities/isDuplicateKeyError.d.ts +7 -0
- package/dist/oauth/utilities/isDuplicateKeyError.js +14 -0
- package/dist/oauth/utilities/refreshTokens.js +3 -3
- package/dist/plugin/build-config.d.ts +21 -14
- package/dist/plugin/build-config.js +208 -17
- package/dist/storage-content-api/api-types.d.ts +168 -0
- package/dist/storage-content-api/api-types.js +6 -0
- package/dist/storage-content-api/client-uploads/ClientUploadHandler.d.ts +9 -0
- package/dist/storage-content-api/client-uploads/ClientUploadHandler.js +42 -0
- package/dist/storage-content-api/client-uploads/generateSignedURL.d.ts +13 -0
- package/dist/storage-content-api/client-uploads/generateSignedURL.js +39 -0
- package/dist/storage-content-api/client.d.ts +6 -0
- package/dist/storage-content-api/client.js +36 -0
- package/dist/storage-content-api/generateURL.d.ts +7 -0
- package/dist/storage-content-api/generateURL.js +8 -0
- package/dist/storage-content-api/handleDelete.d.ts +8 -0
- package/dist/storage-content-api/handleDelete.js +17 -0
- package/dist/storage-content-api/handleUpload.d.ts +11 -0
- package/dist/storage-content-api/handleUpload.js +144 -0
- package/dist/storage-content-api/index.d.ts +5 -0
- package/dist/storage-content-api/index.js +31 -0
- package/dist/storage-content-api/staticHandler.d.ts +10 -0
- package/dist/storage-content-api/staticHandler.js +56 -0
- package/dist/storage-content-api/types.d.ts +10 -0
- package/dist/storage-content-api/types.js +3 -0
- package/dist/storage-content-api/utilities/getSafeFilename.d.ts +12 -0
- package/dist/storage-content-api/utilities/getSafeFilename.js +61 -0
- package/dist/storage-content-api/utilities/index.d.ts +2 -0
- package/dist/storage-content-api/utilities/index.js +3 -0
- package/dist/templates/.env.example +5 -0
- package/dist/templates/README.md +19 -0
- package/dist/types/config.d.ts +2 -1
- package/dist/types/config.js +0 -2
- package/dist/types.d.ts +3 -1
- package/dist/utils/asset-collection.d.ts +3 -6
- package/dist/utils/asset-collection.js +10 -34
- package/dist/utils/build-lambda-zip.d.ts +14 -0
- package/dist/utils/build-lambda-zip.js +81 -0
- package/dist/utils/cache-bootstrap.d.ts +8 -0
- package/dist/utils/cache-bootstrap.js +18 -0
- package/dist/utils/config.js +1 -1
- package/dist/utils/env-management.d.ts +16 -7
- package/dist/utils/env-management.js +85 -64
- package/dist/utils/format-env-suffix.d.ts +12 -0
- package/dist/utils/format-env-suffix.js +13 -0
- package/dist/utils/formatter.js +3 -5
- package/dist/utils/lambda-config.d.ts +3 -1
- package/dist/utils/lambda-config.js +38 -32
- package/dist/utils/messages.d.ts +1 -1
- package/dist/utils/messages.js +22 -8
- package/dist/utils/package-manager.d.ts +24 -0
- package/dist/utils/package-manager.js +53 -0
- package/dist/utils/payload-config-ast.d.ts +22 -3
- package/dist/utils/payload-config-ast.js +96 -31
- package/dist/utils/payload-config-finder.d.ts +2 -2
- package/dist/utils/payload-config-finder.js +48 -9
- package/dist/utils/payload-config-modifier.d.ts +1 -1
- package/dist/utils/payload-config-modifier.js +58 -9
- package/dist/utils/payload-package-check.d.ts +12 -1
- package/dist/utils/payload-package-check.js +63 -5
- package/dist/utils/project.d.ts +3 -2
- package/dist/utils/project.js +15 -7
- package/dist/utils/resolve-environment.d.ts +14 -0
- package/dist/utils/resolve-environment.js +28 -0
- package/dist/utils/s3-upload.d.ts +0 -1
- package/dist/utils/s3-upload.js +10 -3
- package/dist/utils/token-display.js +11 -5
- package/dist/utils/typescript-validator.js +4 -6
- package/dist/utils/version-check.d.ts +3 -0
- package/dist/utils/version-check.js +38 -0
- package/package.json +29 -10
- package/dist/api/control-plane.d.ts.map +0 -1
- package/dist/api/control-plane.js.map +0 -1
- package/dist/api/figma-api.d.ts.map +0 -1
- package/dist/api/figma-api.js.map +0 -1
- package/dist/auth/browser.d.ts.map +0 -1
- package/dist/auth/browser.js.map +0 -1
- package/dist/auth/callback-server.d.ts.map +0 -1
- package/dist/auth/callback-server.js.map +0 -1
- package/dist/auth/crypto-utils.d.ts.map +0 -1
- package/dist/auth/crypto-utils.js.map +0 -1
- package/dist/auth/jwt-validator.d.ts.map +0 -1
- package/dist/auth/jwt-validator.js.map +0 -1
- package/dist/auth/oauth-flow.d.ts.map +0 -1
- package/dist/auth/oauth-flow.js.map +0 -1
- package/dist/auth/pkce.d.ts.map +0 -1
- package/dist/auth/pkce.js.map +0 -1
- package/dist/auth/project-token.d.ts.map +0 -1
- package/dist/auth/project-token.js.map +0 -1
- package/dist/auth/refresh.d.ts.map +0 -1
- package/dist/auth/refresh.js.map +0 -1
- package/dist/auth/token-store.d.ts.map +0 -1
- package/dist/auth/token-store.js.map +0 -1
- package/dist/auth/types.d.ts.map +0 -1
- package/dist/auth/types.js.map +0 -1
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/commands/deploy.d.ts.map +0 -1
- package/dist/commands/deploy.js.map +0 -1
- package/dist/commands/init.d.ts.map +0 -1
- package/dist/commands/init.js.map +0 -1
- package/dist/commands/list-tokens.d.ts.map +0 -1
- package/dist/commands/list-tokens.js.map +0 -1
- package/dist/commands/login.d.ts.map +0 -1
- package/dist/commands/login.js.map +0 -1
- package/dist/commands/logout.d.ts.map +0 -1
- package/dist/commands/logout.js.map +0 -1
- package/dist/config/oauth.d.ts.map +0 -1
- package/dist/config/oauth.js.map +0 -1
- package/dist/constants.d.ts.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/exports/client.d.ts.map +0 -1
- package/dist/exports/client.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/oauth/components/LoginButton/index.d.ts.map +0 -1
- package/dist/oauth/components/LoginButton/index.js.map +0 -1
- package/dist/oauth/components/LogoutButton/index.d.ts.map +0 -1
- package/dist/oauth/components/LogoutButton/index.js.map +0 -1
- package/dist/oauth/defaults.d.ts.map +0 -1
- package/dist/oauth/defaults.js.map +0 -1
- package/dist/oauth/endpoints/getLoginEndpoint.d.ts.map +0 -1
- package/dist/oauth/endpoints/getLoginEndpoint.js.map +0 -1
- package/dist/oauth/endpoints/getLogoutEndpoint.d.ts.map +0 -1
- package/dist/oauth/endpoints/getLogoutEndpoint.js.map +0 -1
- package/dist/oauth/endpoints/getMetaEndpoint.d.ts.map +0 -1
- package/dist/oauth/endpoints/getMetaEndpoint.js.map +0 -1
- package/dist/oauth/endpoints/getRedirectToLoginEndpoint.d.ts.map +0 -1
- package/dist/oauth/endpoints/getRedirectToLoginEndpoint.js.map +0 -1
- package/dist/oauth/exports/client.d.ts.map +0 -1
- package/dist/oauth/exports/client.js.map +0 -1
- package/dist/oauth/hooks/afterLogout.d.ts.map +0 -1
- package/dist/oauth/hooks/afterLogout.js.map +0 -1
- package/dist/oauth/hooks/me.d.ts.map +0 -1
- package/dist/oauth/hooks/me.js.map +0 -1
- package/dist/oauth/hooks/refresh.d.ts.map +0 -1
- package/dist/oauth/hooks/refresh.js.map +0 -1
- package/dist/oauth/index.d.ts.map +0 -1
- package/dist/oauth/index.js.map +0 -1
- package/dist/oauth/strategy/index.d.ts.map +0 -1
- package/dist/oauth/strategy/index.js.map +0 -1
- package/dist/oauth/types.d.ts.map +0 -1
- package/dist/oauth/types.js.map +0 -1
- package/dist/oauth/utilities/clearCookie.d.ts.map +0 -1
- package/dist/oauth/utilities/clearCookie.js.map +0 -1
- package/dist/oauth/utilities/createCookieOptions.d.ts.map +0 -1
- package/dist/oauth/utilities/createCookieOptions.js.map +0 -1
- package/dist/oauth/utilities/createDebugLogger.d.ts.map +0 -1
- package/dist/oauth/utilities/createDebugLogger.js.map +0 -1
- package/dist/oauth/utilities/extractOrigin.d.ts.map +0 -1
- package/dist/oauth/utilities/extractOrigin.js.map +0 -1
- package/dist/oauth/utilities/getAdminCollectionSlug.d.ts.map +0 -1
- package/dist/oauth/utilities/getAdminCollectionSlug.js.map +0 -1
- package/dist/oauth/utilities/getAdminPath.d.ts +0 -8
- package/dist/oauth/utilities/getAdminPath.d.ts.map +0 -1
- package/dist/oauth/utilities/getAdminPath.js +0 -9
- package/dist/oauth/utilities/getAdminPath.js.map +0 -1
- package/dist/oauth/utilities/getAuthorizeURL.d.ts.map +0 -1
- package/dist/oauth/utilities/getAuthorizeURL.js.map +0 -1
- package/dist/oauth/utilities/getCookieExpiration.d.ts.map +0 -1
- package/dist/oauth/utilities/getCookieExpiration.js.map +0 -1
- package/dist/oauth/utilities/getSecondsFromTokenExp.d.ts.map +0 -1
- package/dist/oauth/utilities/getSecondsFromTokenExp.js.map +0 -1
- package/dist/oauth/utilities/getTokenExp.d.ts.map +0 -1
- package/dist/oauth/utilities/getTokenExp.js.map +0 -1
- package/dist/oauth/utilities/getUsernameField.d.ts.map +0 -1
- package/dist/oauth/utilities/getUsernameField.js.map +0 -1
- package/dist/oauth/utilities/hasUserTokenPropsChanged.d.ts.map +0 -1
- package/dist/oauth/utilities/hasUserTokenPropsChanged.js.map +0 -1
- package/dist/oauth/utilities/mergeHeaders.d.ts.map +0 -1
- package/dist/oauth/utilities/mergeHeaders.js.map +0 -1
- package/dist/oauth/utilities/refreshTokens.d.ts.map +0 -1
- package/dist/oauth/utilities/refreshTokens.js.map +0 -1
- package/dist/plugin/adapter.d.ts.map +0 -1
- package/dist/plugin/adapter.js.map +0 -1
- package/dist/plugin/build-config.d.ts.map +0 -1
- package/dist/plugin/build-config.js.map +0 -1
- package/dist/types/config.d.ts.map +0 -1
- package/dist/types/config.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/utils/asset-collection.d.ts.map +0 -1
- package/dist/utils/asset-collection.js.map +0 -1
- package/dist/utils/build-detection.d.ts.map +0 -1
- package/dist/utils/build-detection.js.map +0 -1
- package/dist/utils/config.d.ts.map +0 -1
- package/dist/utils/config.js.map +0 -1
- package/dist/utils/download-template.d.ts.map +0 -1
- package/dist/utils/download-template.js.map +0 -1
- package/dist/utils/env-management.d.ts.map +0 -1
- package/dist/utils/env-management.js.map +0 -1
- package/dist/utils/format.d.ts.map +0 -1
- package/dist/utils/format.js.map +0 -1
- package/dist/utils/formatter.d.ts.map +0 -1
- package/dist/utils/formatter.js.map +0 -1
- package/dist/utils/git.d.ts.map +0 -1
- package/dist/utils/git.js.map +0 -1
- package/dist/utils/is-debug.d.ts.map +0 -1
- package/dist/utils/is-debug.js.map +0 -1
- package/dist/utils/lambda-config.d.ts.map +0 -1
- package/dist/utils/lambda-config.js.map +0 -1
- package/dist/utils/log.d.ts.map +0 -1
- package/dist/utils/log.js.map +0 -1
- package/dist/utils/messages.d.ts.map +0 -1
- package/dist/utils/messages.js.map +0 -1
- package/dist/utils/package-manager.d.ts.map +0 -1
- package/dist/utils/package-manager.js.map +0 -1
- package/dist/utils/payload-config-ast.d.ts.map +0 -1
- package/dist/utils/payload-config-ast.js.map +0 -1
- package/dist/utils/payload-config-finder.d.ts.map +0 -1
- package/dist/utils/payload-config-finder.js.map +0 -1
- package/dist/utils/payload-config-modifier.d.ts.map +0 -1
- package/dist/utils/payload-config-modifier.js.map +0 -1
- package/dist/utils/payload-package-check.d.ts.map +0 -1
- package/dist/utils/payload-package-check.js.map +0 -1
- package/dist/utils/project.d.ts.map +0 -1
- package/dist/utils/project.js.map +0 -1
- package/dist/utils/s3-upload.d.ts.map +0 -1
- package/dist/utils/s3-upload.js.map +0 -1
- package/dist/utils/token-display.d.ts.map +0 -1
- package/dist/utils/token-display.js.map +0 -1
- package/dist/utils/typescript-validator.d.ts.map +0 -1
- package/dist/utils/typescript-validator.js.map +0 -1
|
@@ -0,0 +1,702 @@
|
|
|
1
|
+
import createClient from 'openapi-fetch';
|
|
2
|
+
import { createDatabaseAdapter } from 'payload';
|
|
3
|
+
import { v4 as uuid } from 'uuid';
|
|
4
|
+
import { FigmaApiError } from '../api/figma-api.js';
|
|
5
|
+
import { CONTENT_SYSTEM_NOT_FOUND_MESSAGE } from '../constants.js';
|
|
6
|
+
import { getGlobalSlug } from './temp-utilities/slug.js';
|
|
7
|
+
import { addFallbackSort } from './temp-utilities/sorting.js';
|
|
8
|
+
import { unwrapDocument, unwrapFindResponse } from './temp-utilities/unwrapDocument.js';
|
|
9
|
+
import { createAuthMiddleware, createErrorMiddleware } from './utilities/auth.js';
|
|
10
|
+
import { dataToContentAPI } from './utilities/data/index.js';
|
|
11
|
+
import { convertPayloadJoinsToContentAPI } from './utilities/joins.js';
|
|
12
|
+
import { addFallbackLocale } from './utilities/locale/index.js';
|
|
13
|
+
import { buildMeta } from './utilities/meta/buildMeta.js';
|
|
14
|
+
import { convertPayloadWhereToContentAPI } from './utilities/where.js';
|
|
15
|
+
async function syncCollections() {
|
|
16
|
+
let existingIds;
|
|
17
|
+
try {
|
|
18
|
+
const { data: response, error, response: httpResponse } = await this.client.GET('/api/v0/content_systems/{contentSystemId}/collections', {
|
|
19
|
+
params: {
|
|
20
|
+
path: {
|
|
21
|
+
contentSystemId: this.contentSystemId
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
if (httpResponse.status === 404 || httpResponse.status === 410) {
|
|
26
|
+
throw new Error(CONTENT_SYSTEM_NOT_FOUND_MESSAGE);
|
|
27
|
+
}
|
|
28
|
+
if (error) {
|
|
29
|
+
throw new Error(JSON.stringify(error));
|
|
30
|
+
}
|
|
31
|
+
if (!response) {
|
|
32
|
+
throw new Error('No response from collections endpoint');
|
|
33
|
+
}
|
|
34
|
+
existingIds = new Set(response.data.map((c)=>c.id));
|
|
35
|
+
this.payload.logger.info(`Found ${existingIds.size} existing collections`);
|
|
36
|
+
} catch (error) {
|
|
37
|
+
if (error instanceof Error && error.message === CONTENT_SYSTEM_NOT_FOUND_MESSAGE) {
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
if (error instanceof FigmaApiError && (error.statusCode === 404 || error.statusCode === 410)) {
|
|
41
|
+
throw new Error(CONTENT_SYSTEM_NOT_FOUND_MESSAGE);
|
|
42
|
+
}
|
|
43
|
+
this.payload.logger.warn(`Failed to fetch collections, will attempt to create all: ${error.message}`);
|
|
44
|
+
existingIds = new Set();
|
|
45
|
+
}
|
|
46
|
+
for (const collection of this.payload.config.collections){
|
|
47
|
+
if (!existingIds.has(collection.slug)) {
|
|
48
|
+
await createCollection.call(this, collection.slug);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
for (const global of this.payload.config.globals || []){
|
|
52
|
+
const globalId = `_global-${global.slug}`;
|
|
53
|
+
if (!existingIds.has(globalId)) {
|
|
54
|
+
await createCollection.call(this, globalId);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async function init() {
|
|
59
|
+
if (this.auth.mode === 'apiKey') {
|
|
60
|
+
this.payload.logger.info('Using API Key authentication');
|
|
61
|
+
} else if (this.auth.mode === 'tokenStore') {
|
|
62
|
+
this.payload.logger.info('Using TokenStore authentication (local dev)');
|
|
63
|
+
} else {
|
|
64
|
+
this.payload.logger.info('Using Dev JWT authentication (testing)');
|
|
65
|
+
}
|
|
66
|
+
if (process.env.PAYLOAD_DROP_DATABASE === 'true') {
|
|
67
|
+
await this.clearDatabase();
|
|
68
|
+
}
|
|
69
|
+
await syncCollections.call(this);
|
|
70
|
+
}
|
|
71
|
+
async function createCollection(collectionId) {
|
|
72
|
+
try {
|
|
73
|
+
const { error } = await this.client.POST('/api/v0/content_systems/{contentSystemId}/collections', {
|
|
74
|
+
body: {
|
|
75
|
+
id: collectionId
|
|
76
|
+
},
|
|
77
|
+
params: {
|
|
78
|
+
path: {
|
|
79
|
+
contentSystemId: this.contentSystemId
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
if (error) {
|
|
84
|
+
throw new Error(JSON.stringify(error));
|
|
85
|
+
}
|
|
86
|
+
this.payload.logger.info(`Created collection: ${collectionId}`);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
const errorMessage = error.message;
|
|
89
|
+
if (!errorMessage.includes('already exists') && !errorMessage.includes('409')) {
|
|
90
|
+
this.payload.logger.warn(`Failed to create collection ${collectionId}: ${errorMessage}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async function findMany({ collection, joins, limit, locale: localeArg, page, pagination: _pagination, sort, where }) {
|
|
95
|
+
const locale = addFallbackLocale(localeArg, this.payload);
|
|
96
|
+
const { data: response, error } = await this.client.POST('/api/v0/documents:find', {
|
|
97
|
+
body: {
|
|
98
|
+
collection,
|
|
99
|
+
contentSystemId: this.contentSystemId,
|
|
100
|
+
join: convertPayloadJoinsToContentAPI(this.payload, collection, joins),
|
|
101
|
+
limit,
|
|
102
|
+
locale,
|
|
103
|
+
page: page ?? 1,
|
|
104
|
+
sort: addFallbackSort(sort, this.payload, collection),
|
|
105
|
+
where: convertPayloadWhereToContentAPI(where ?? {}),
|
|
106
|
+
...buildMeta(this.payload, {
|
|
107
|
+
collection,
|
|
108
|
+
locale,
|
|
109
|
+
where
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
if (error) {
|
|
114
|
+
throw new Error(`Content API findMany error: ${JSON.stringify(error)}`);
|
|
115
|
+
}
|
|
116
|
+
if (!response) {
|
|
117
|
+
throw new Error('No response from findMany');
|
|
118
|
+
}
|
|
119
|
+
return unwrapFindResponse(this.payload, response.result, collection);
|
|
120
|
+
}
|
|
121
|
+
async function find(args) {
|
|
122
|
+
const collectionConfig = this.payload.config.collections.find((c)=>c.slug === args.collection);
|
|
123
|
+
return findMany.call(this, {
|
|
124
|
+
collection: args.collection,
|
|
125
|
+
joins: args.joins,
|
|
126
|
+
// TODO: FindArgs.limit should be 'number' not 'number | undefined'
|
|
127
|
+
// is sanitized by Payload Core before calling the adapter (defaults to 10 or 0 based on pagination)
|
|
128
|
+
limit: args.limit,
|
|
129
|
+
locale: args.locale,
|
|
130
|
+
page: args.page ?? 1,
|
|
131
|
+
pagination: args.pagination,
|
|
132
|
+
sort: args.sort || collectionConfig?.defaultSort,
|
|
133
|
+
where: args.where
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
// TODO: remove Omit when this PR is released: https://github.com/payloadcms/payload/pull/15183
|
|
137
|
+
async function findVersions(args) {
|
|
138
|
+
const sortWithFallback = addFallbackSort(args.sort || '-updatedAt', this.payload, args.collection);
|
|
139
|
+
const whereClause = convertPayloadWhereToContentAPI(args.where ?? {}, {
|
|
140
|
+
parentToDocumentId: true,
|
|
141
|
+
stripVersionPrefix: true
|
|
142
|
+
});
|
|
143
|
+
const locale = addFallbackLocale(args.locale, this.payload);
|
|
144
|
+
const { data: response, error } = await this.client.POST('/api/v0/document_versions:find', {
|
|
145
|
+
body: {
|
|
146
|
+
collection: args.collection,
|
|
147
|
+
contentSystemId: this.contentSystemId,
|
|
148
|
+
// TODO: FindVersionsArgs.limit should be 'number' not 'number | undefined'
|
|
149
|
+
// is sanitized by Payload Core before calling the adapter (defaults to 10 or 0 based on pagination)
|
|
150
|
+
limit: args.limit,
|
|
151
|
+
locale,
|
|
152
|
+
page: args.page ?? 1,
|
|
153
|
+
sort: sortWithFallback,
|
|
154
|
+
where: whereClause,
|
|
155
|
+
...buildMeta(this.payload, {
|
|
156
|
+
collection: args.collection,
|
|
157
|
+
locale,
|
|
158
|
+
where: args.where
|
|
159
|
+
})
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
if (error) {
|
|
163
|
+
throw new Error(`Content API findVersions error: ${JSON.stringify(error)}`);
|
|
164
|
+
}
|
|
165
|
+
if (!response) {
|
|
166
|
+
throw new Error('No response from findVersions');
|
|
167
|
+
}
|
|
168
|
+
return unwrapFindResponse(this.payload, response.result, args.collection);
|
|
169
|
+
}
|
|
170
|
+
async function queryDrafts(args) {
|
|
171
|
+
const versionsResult = await this.findVersions({
|
|
172
|
+
collection: args.collection,
|
|
173
|
+
limit: args.limit,
|
|
174
|
+
locale: args.locale,
|
|
175
|
+
page: args.page,
|
|
176
|
+
pagination: args.pagination,
|
|
177
|
+
sort: args.sort,
|
|
178
|
+
where: {
|
|
179
|
+
...args.where,
|
|
180
|
+
latest: {
|
|
181
|
+
equals: true
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
// queryDrafts returns documents at root level, not nested in 'version'.
|
|
186
|
+
// Only spread version content; version metadata (latest, parent) must not leak.
|
|
187
|
+
const docs = versionsResult.docs.map((versionDoc)=>({
|
|
188
|
+
...versionDoc.version,
|
|
189
|
+
id: versionDoc.parent
|
|
190
|
+
}));
|
|
191
|
+
return {
|
|
192
|
+
...versionsResult,
|
|
193
|
+
docs
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
async function createVersion(args) {
|
|
197
|
+
const { data: response, error } = await this.client.POST('/api/v0/document_versions:create', {
|
|
198
|
+
body: {
|
|
199
|
+
collection: args.collectionSlug,
|
|
200
|
+
contentSystemId: this.contentSystemId,
|
|
201
|
+
versionDoc: {
|
|
202
|
+
createdAt: args.createdAt,
|
|
203
|
+
latest: !args.autosave,
|
|
204
|
+
parent: String(args.parent),
|
|
205
|
+
updatedAt: args.updatedAt,
|
|
206
|
+
version: dataToContentAPI(this.payload, args.collectionSlug, args.versionData, {
|
|
207
|
+
applyDefaults: true,
|
|
208
|
+
publishedLocale: args.publishedLocale
|
|
209
|
+
})
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
if (error) {
|
|
214
|
+
throw new Error(`Content API createVersion error: ${JSON.stringify(error)}`);
|
|
215
|
+
}
|
|
216
|
+
if (!response) {
|
|
217
|
+
throw new Error('No response from createVersion');
|
|
218
|
+
}
|
|
219
|
+
return unwrapDocument(this.payload, response.result, args.collectionSlug);
|
|
220
|
+
}
|
|
221
|
+
async function updateVersion(args) {
|
|
222
|
+
const where = args.where ?? {
|
|
223
|
+
id: {
|
|
224
|
+
equals: args.id
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
const locale = addFallbackLocale(args.locale, this.payload);
|
|
228
|
+
// versionData contains version metadata plus nested version content
|
|
229
|
+
const { publishedLocale, version, ...versionMeta } = args.versionData;
|
|
230
|
+
const { data: response, error } = await this.client.POST('/api/v0/document_versions:update', {
|
|
231
|
+
body: {
|
|
232
|
+
collection: args.collection,
|
|
233
|
+
contentSystemId: this.contentSystemId,
|
|
234
|
+
createOnMissing: false,
|
|
235
|
+
locale,
|
|
236
|
+
versionDoc: {
|
|
237
|
+
createdAt: versionMeta.createdAt,
|
|
238
|
+
latest: versionMeta.latest,
|
|
239
|
+
parent: versionMeta.parent != null ? String(versionMeta.parent) : undefined,
|
|
240
|
+
updatedAt: versionMeta.updatedAt,
|
|
241
|
+
version: dataToContentAPI(this.payload, args.collection, version, {
|
|
242
|
+
publishedLocale
|
|
243
|
+
})
|
|
244
|
+
},
|
|
245
|
+
where: convertPayloadWhereToContentAPI(where, {
|
|
246
|
+
stripVersionPrefix: true
|
|
247
|
+
}),
|
|
248
|
+
...buildMeta(this.payload, {
|
|
249
|
+
collection: args.collection,
|
|
250
|
+
locale,
|
|
251
|
+
where
|
|
252
|
+
})
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
if (error) {
|
|
256
|
+
throw new Error(`Content API updateVersion error: ${JSON.stringify(error)}`);
|
|
257
|
+
}
|
|
258
|
+
if (!response?.result || !('data' in response.result)) {
|
|
259
|
+
throw new Error('No document data in updateVersion response');
|
|
260
|
+
}
|
|
261
|
+
const doc = response.result.data[0];
|
|
262
|
+
if (!doc) {
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
return unwrapDocument(this.payload, doc, args.collection);
|
|
266
|
+
}
|
|
267
|
+
async function deleteVersions(args) {
|
|
268
|
+
if (!args.collection) {
|
|
269
|
+
throw new Error('deleteVersions requires a collection');
|
|
270
|
+
}
|
|
271
|
+
const locale = addFallbackLocale(args.locale, this.payload);
|
|
272
|
+
const { error } = await this.client.POST('/api/v0/document_versions:delete', {
|
|
273
|
+
body: {
|
|
274
|
+
collection: args.collection,
|
|
275
|
+
contentSystemId: this.contentSystemId,
|
|
276
|
+
locale,
|
|
277
|
+
where: convertPayloadWhereToContentAPI(args.where, {
|
|
278
|
+
stripVersionPrefix: true
|
|
279
|
+
}),
|
|
280
|
+
...buildMeta(this.payload, {
|
|
281
|
+
collection: args.collection,
|
|
282
|
+
locale,
|
|
283
|
+
where: args.where
|
|
284
|
+
})
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
if (error) {
|
|
288
|
+
throw new Error(`Content API deleteVersions error: ${JSON.stringify(error)}`);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
async function findOne(args) {
|
|
292
|
+
const { docs } = await this.find({
|
|
293
|
+
collection: args.collection,
|
|
294
|
+
joins: args.joins,
|
|
295
|
+
limit: 1,
|
|
296
|
+
locale: args.locale,
|
|
297
|
+
pagination: false,
|
|
298
|
+
where: args.where
|
|
299
|
+
});
|
|
300
|
+
return docs[0] ?? null;
|
|
301
|
+
}
|
|
302
|
+
async function updateMany(args) {
|
|
303
|
+
const locale = addFallbackLocale(args.locale, this.payload);
|
|
304
|
+
const { data: response, error } = await this.client.POST('/api/v0/documents:update', {
|
|
305
|
+
body: {
|
|
306
|
+
collection: args.collection,
|
|
307
|
+
contentSystemId: this.contentSystemId,
|
|
308
|
+
createOnMissing: false,
|
|
309
|
+
doc: dataToContentAPI(this.payload, args.collection, args.data),
|
|
310
|
+
limit: args.limit,
|
|
311
|
+
locale,
|
|
312
|
+
returning: {},
|
|
313
|
+
sort: addFallbackSort(args.sort || '-updatedAt', this.payload, args.collection),
|
|
314
|
+
where: convertPayloadWhereToContentAPI(args.where),
|
|
315
|
+
...buildMeta(this.payload, {
|
|
316
|
+
collection: args.collection,
|
|
317
|
+
locale,
|
|
318
|
+
where: args.where
|
|
319
|
+
})
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
if (error) {
|
|
323
|
+
throw new Error(`Content API updateMany error: ${JSON.stringify(error)}`);
|
|
324
|
+
}
|
|
325
|
+
if (!response) {
|
|
326
|
+
throw new Error('No response from updateMany');
|
|
327
|
+
}
|
|
328
|
+
if (response.result && 'data' in response.result) {
|
|
329
|
+
return response.result.data.map((doc)=>unwrapDocument(this.payload, doc, args.collection));
|
|
330
|
+
}
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
333
|
+
async function updateOne(args) {
|
|
334
|
+
const locale = addFallbackLocale(args.locale, this.payload);
|
|
335
|
+
const where = args.where ?? {
|
|
336
|
+
id: {
|
|
337
|
+
equals: args.id
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
const { data: response, error } = await this.client.POST('/api/v0/documents:update', {
|
|
341
|
+
body: {
|
|
342
|
+
collection: args.collection,
|
|
343
|
+
contentSystemId: this.contentSystemId,
|
|
344
|
+
createOnMissing: false,
|
|
345
|
+
doc: dataToContentAPI(this.payload, args.collection, args.data),
|
|
346
|
+
locale,
|
|
347
|
+
returning: {},
|
|
348
|
+
where: convertPayloadWhereToContentAPI(where),
|
|
349
|
+
...buildMeta(this.payload, {
|
|
350
|
+
collection: args.collection,
|
|
351
|
+
locale,
|
|
352
|
+
where
|
|
353
|
+
})
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
if (error) {
|
|
357
|
+
throw new Error(`Content API updateOne error: ${JSON.stringify(error)}`);
|
|
358
|
+
}
|
|
359
|
+
if (!response) {
|
|
360
|
+
throw new Error('No response from updateOne');
|
|
361
|
+
}
|
|
362
|
+
if (!response.result || !('data' in response.result)) {
|
|
363
|
+
throw new Error('Unexpected response format from updateOne');
|
|
364
|
+
}
|
|
365
|
+
const doc = response.result.data[0];
|
|
366
|
+
if (!doc) {
|
|
367
|
+
return null;
|
|
368
|
+
}
|
|
369
|
+
return unwrapDocument(this.payload, doc, args.collection);
|
|
370
|
+
}
|
|
371
|
+
async function deleteMany(args) {
|
|
372
|
+
const locale = addFallbackLocale(args.req?.locale, this.payload);
|
|
373
|
+
const { error } = await this.client.POST('/api/v0/documents:delete', {
|
|
374
|
+
body: {
|
|
375
|
+
collection: args.collection,
|
|
376
|
+
contentSystemId: this.contentSystemId,
|
|
377
|
+
locale,
|
|
378
|
+
where: convertPayloadWhereToContentAPI(args.where),
|
|
379
|
+
...buildMeta(this.payload, {
|
|
380
|
+
collection: args.collection,
|
|
381
|
+
locale,
|
|
382
|
+
where: args.where
|
|
383
|
+
})
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
if (error) {
|
|
387
|
+
throw new Error(`Content API deleteMany error: ${JSON.stringify(error)}`);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
async function deleteOne(args) {
|
|
391
|
+
const locale = addFallbackLocale(args.req?.locale, this.payload);
|
|
392
|
+
const { data: response, error } = await this.client.POST('/api/v0/documents:delete', {
|
|
393
|
+
body: {
|
|
394
|
+
collection: args.collection,
|
|
395
|
+
contentSystemId: this.contentSystemId,
|
|
396
|
+
locale,
|
|
397
|
+
returning: {},
|
|
398
|
+
where: convertPayloadWhereToContentAPI(args.where),
|
|
399
|
+
...buildMeta(this.payload, {
|
|
400
|
+
collection: args.collection,
|
|
401
|
+
locale,
|
|
402
|
+
where: args.where
|
|
403
|
+
})
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
if (error) {
|
|
407
|
+
throw new Error(`Content API deleteOne error: ${JSON.stringify(error)}`);
|
|
408
|
+
}
|
|
409
|
+
if (!response?.result || !('data' in response.result)) {
|
|
410
|
+
throw new Error('No document data in deleteOne response');
|
|
411
|
+
}
|
|
412
|
+
const doc = response.result.data[0];
|
|
413
|
+
if (!doc) {
|
|
414
|
+
return null;
|
|
415
|
+
}
|
|
416
|
+
return unwrapDocument(this.payload, doc, args.collection);
|
|
417
|
+
}
|
|
418
|
+
async function create(args) {
|
|
419
|
+
const isGlobalCollection = args.collection.startsWith('_global-');
|
|
420
|
+
const customIDType = this.payload.collections[args.collection]?.customIDType;
|
|
421
|
+
const id = (isGlobalCollection || customIDType || this.allowIDOnCreate) && args.data.id != null && (typeof args.data.id === 'string' || typeof args.data.id === 'number') ? String(args.data.id) : uuid();
|
|
422
|
+
const locale = addFallbackLocale(args.locale, this.payload);
|
|
423
|
+
const { data: response, error } = await this.client.POST('/api/v0/documents:create', {
|
|
424
|
+
body: {
|
|
425
|
+
collection: args.collection,
|
|
426
|
+
contentSystemId: this.contentSystemId,
|
|
427
|
+
doc: {
|
|
428
|
+
...dataToContentAPI(this.payload, args.collection, args.data, {
|
|
429
|
+
applyDefaults: true,
|
|
430
|
+
createdAt: true
|
|
431
|
+
}),
|
|
432
|
+
id
|
|
433
|
+
},
|
|
434
|
+
locale,
|
|
435
|
+
...buildMeta(this.payload, {
|
|
436
|
+
collection: args.collection,
|
|
437
|
+
locale
|
|
438
|
+
})
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
if (error) {
|
|
442
|
+
throw new Error(`Content API create error: ${JSON.stringify(error)}`);
|
|
443
|
+
}
|
|
444
|
+
if (!response) {
|
|
445
|
+
throw new Error('No response from create');
|
|
446
|
+
}
|
|
447
|
+
return unwrapDocument(this.payload, response.result, args.collection);
|
|
448
|
+
}
|
|
449
|
+
async function clearDatabase() {
|
|
450
|
+
this.payload.logger.info(`---- CLEARING CONTENT API DATABASE ----`);
|
|
451
|
+
try {
|
|
452
|
+
await fetch(`${this.url}/dev/clear-db`, {
|
|
453
|
+
headers: {
|
|
454
|
+
'Content-Type': 'application/json'
|
|
455
|
+
},
|
|
456
|
+
method: 'POST'
|
|
457
|
+
});
|
|
458
|
+
this.payload.logger.info('---- CONTENT API DATABASE CLEARED ----');
|
|
459
|
+
// Re-sync collections after clearing
|
|
460
|
+
await syncCollections.call(this);
|
|
461
|
+
} catch (error) {
|
|
462
|
+
this.payload.logger.error(`Failed to clear database: ${error.message}`);
|
|
463
|
+
throw error;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
async function count(args) {
|
|
467
|
+
const locale = addFallbackLocale(args.locale, this.payload);
|
|
468
|
+
const { data: response, error } = await this.client.POST('/api/v0/documents:count', {
|
|
469
|
+
body: {
|
|
470
|
+
collection: args.collection,
|
|
471
|
+
contentSystemId: this.contentSystemId,
|
|
472
|
+
locale,
|
|
473
|
+
where: convertPayloadWhereToContentAPI(args.where),
|
|
474
|
+
...buildMeta(this.payload, {
|
|
475
|
+
collection: args.collection,
|
|
476
|
+
locale,
|
|
477
|
+
where: args.where
|
|
478
|
+
})
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
if (error) {
|
|
482
|
+
throw new Error(`Content API count error: ${JSON.stringify(error)}`);
|
|
483
|
+
}
|
|
484
|
+
if (!response) {
|
|
485
|
+
throw new Error('No response from count');
|
|
486
|
+
}
|
|
487
|
+
return {
|
|
488
|
+
totalDocs: response.result.count
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
async function countVersions(args) {
|
|
492
|
+
const locale = addFallbackLocale(args.locale, this.payload);
|
|
493
|
+
const { data: response, error } = await this.client.POST('/api/v0/document_versions:count', {
|
|
494
|
+
body: {
|
|
495
|
+
collection: args.collection,
|
|
496
|
+
contentSystemId: this.contentSystemId,
|
|
497
|
+
locale,
|
|
498
|
+
where: convertPayloadWhereToContentAPI(args.where, {
|
|
499
|
+
stripVersionPrefix: true
|
|
500
|
+
}),
|
|
501
|
+
...buildMeta(this.payload, {
|
|
502
|
+
collection: args.collection,
|
|
503
|
+
locale,
|
|
504
|
+
where: args.where
|
|
505
|
+
})
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
if (error) {
|
|
509
|
+
throw new Error(`Content API countVersions error: ${JSON.stringify(error)}`);
|
|
510
|
+
}
|
|
511
|
+
if (!response) {
|
|
512
|
+
throw new Error('No response from countVersions');
|
|
513
|
+
}
|
|
514
|
+
return {
|
|
515
|
+
totalDocs: response.result.count
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
async function upsert(args) {
|
|
519
|
+
let documentId = args.data.id;
|
|
520
|
+
if (!documentId && args.where && 'id' in args.where) {
|
|
521
|
+
const idClause = args.where.id;
|
|
522
|
+
if (idClause && 'equals' in idClause) {
|
|
523
|
+
documentId = idClause.equals;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
if (!documentId) {
|
|
527
|
+
documentId = uuid();
|
|
528
|
+
}
|
|
529
|
+
const locale = addFallbackLocale(args.locale, this.payload);
|
|
530
|
+
const { data: response, error } = await this.client.POST('/api/v0/documents:update', {
|
|
531
|
+
body: {
|
|
532
|
+
collection: args.collection,
|
|
533
|
+
contentSystemId: this.contentSystemId,
|
|
534
|
+
createOnMissing: {
|
|
535
|
+
id: documentId
|
|
536
|
+
},
|
|
537
|
+
doc: dataToContentAPI(this.payload, args.collection, args.data, {
|
|
538
|
+
applyDefaults: true,
|
|
539
|
+
createdAt: true
|
|
540
|
+
}),
|
|
541
|
+
locale,
|
|
542
|
+
where: convertPayloadWhereToContentAPI(args.where),
|
|
543
|
+
...buildMeta(this.payload, {
|
|
544
|
+
collection: args.collection,
|
|
545
|
+
locale,
|
|
546
|
+
where: args.where
|
|
547
|
+
})
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
if (error) {
|
|
551
|
+
throw new Error(`Content API upsert error: ${JSON.stringify(error)}`);
|
|
552
|
+
}
|
|
553
|
+
if (!response) {
|
|
554
|
+
throw new Error('No response from upsert');
|
|
555
|
+
}
|
|
556
|
+
if (!response.result || !('data' in response.result)) {
|
|
557
|
+
throw new Error('Unexpected response format from upsert');
|
|
558
|
+
}
|
|
559
|
+
const doc = response.result.data[0];
|
|
560
|
+
if (!doc) {
|
|
561
|
+
throw new Error('No document data in upsert response');
|
|
562
|
+
}
|
|
563
|
+
return unwrapDocument(this.payload, doc, args.collection);
|
|
564
|
+
}
|
|
565
|
+
function createGlobal(args) {
|
|
566
|
+
return this.create({
|
|
567
|
+
collection: getGlobalSlug(args.slug),
|
|
568
|
+
data: {
|
|
569
|
+
...args.data,
|
|
570
|
+
id: args.slug,
|
|
571
|
+
globalType: args.slug
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
function findGlobal(args) {
|
|
576
|
+
return this.findOne({
|
|
577
|
+
collection: getGlobalSlug(args.slug),
|
|
578
|
+
locale: args.locale,
|
|
579
|
+
where: args.where ?? {}
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
async function updateGlobal(args) {
|
|
583
|
+
// Use upsert to ensure the global exists
|
|
584
|
+
// Use slug as the consistent document ID for globals
|
|
585
|
+
return this.upsert({
|
|
586
|
+
collection: getGlobalSlug(args.slug),
|
|
587
|
+
data: args.data,
|
|
588
|
+
where: {
|
|
589
|
+
id: {
|
|
590
|
+
equals: args.slug
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
// TODO: remove Omit when this PR is released: https://github.com/payloadcms/payload/pull/15183
|
|
596
|
+
function findGlobalVersions(args) {
|
|
597
|
+
return this.findVersions({
|
|
598
|
+
collection: getGlobalSlug(args.global),
|
|
599
|
+
limit: args.limit,
|
|
600
|
+
locale: args.locale,
|
|
601
|
+
page: args.page,
|
|
602
|
+
pagination: args.pagination,
|
|
603
|
+
sort: args.sort,
|
|
604
|
+
where: args.where
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
function createGlobalVersion(args) {
|
|
608
|
+
// For globals, use the globalSlug as the consistent document ID
|
|
609
|
+
// This matches what we do in updateGlobal
|
|
610
|
+
return this.createVersion({
|
|
611
|
+
autosave: args.autosave,
|
|
612
|
+
collectionSlug: getGlobalSlug(args.globalSlug),
|
|
613
|
+
createdAt: args.createdAt,
|
|
614
|
+
parent: args.globalSlug,
|
|
615
|
+
publishedLocale: args.publishedLocale,
|
|
616
|
+
updatedAt: args.updatedAt,
|
|
617
|
+
versionData: args.versionData
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
function updateGlobalVersion(args) {
|
|
621
|
+
// UpdateVersion requires either id OR where, not both
|
|
622
|
+
if (args.id !== undefined) {
|
|
623
|
+
return this.updateVersion({
|
|
624
|
+
id: args.id,
|
|
625
|
+
collection: getGlobalSlug(args.global),
|
|
626
|
+
locale: args.locale,
|
|
627
|
+
versionData: args.versionData
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
// args.where is guaranteed by Payload when args.id is undefined
|
|
631
|
+
return this.updateVersion({
|
|
632
|
+
collection: getGlobalSlug(args.global),
|
|
633
|
+
locale: args.locale,
|
|
634
|
+
versionData: args.versionData,
|
|
635
|
+
where: args.where
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
function countGlobalVersions(args) {
|
|
639
|
+
return this.countVersions({
|
|
640
|
+
collection: getGlobalSlug(args.global),
|
|
641
|
+
locale: args.locale,
|
|
642
|
+
where: args.where
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
export const contentAPIAdapter = (opts)=>({
|
|
646
|
+
name: 'content-api',
|
|
647
|
+
defaultIDType: 'text',
|
|
648
|
+
init: ({ payload })=>{
|
|
649
|
+
const client = createClient({
|
|
650
|
+
baseUrl: opts.url
|
|
651
|
+
});
|
|
652
|
+
const middlewareOpts = {
|
|
653
|
+
auth: opts.auth,
|
|
654
|
+
contentSystemId: opts.contentSystemId,
|
|
655
|
+
logger: payload.logger,
|
|
656
|
+
url: opts.url
|
|
657
|
+
};
|
|
658
|
+
client.use(createAuthMiddleware(middlewareOpts));
|
|
659
|
+
client.use(createErrorMiddleware(middlewareOpts));
|
|
660
|
+
return createDatabaseAdapter({
|
|
661
|
+
name: 'content-api',
|
|
662
|
+
allowIDOnCreate: opts.allowIDOnCreate ?? false,
|
|
663
|
+
auth: opts.auth,
|
|
664
|
+
beginTransaction: ()=>Promise.resolve(null),
|
|
665
|
+
clearDatabase: clearDatabase,
|
|
666
|
+
client,
|
|
667
|
+
commitTransaction: async ()=>{},
|
|
668
|
+
contentSystemId: opts.contentSystemId,
|
|
669
|
+
count: count,
|
|
670
|
+
countGlobalVersions: countGlobalVersions,
|
|
671
|
+
countVersions: countVersions,
|
|
672
|
+
create: create,
|
|
673
|
+
createGlobal: createGlobal,
|
|
674
|
+
createGlobalVersion: createGlobalVersion,
|
|
675
|
+
createVersion: createVersion,
|
|
676
|
+
defaultIDType: 'text',
|
|
677
|
+
deleteMany: deleteMany,
|
|
678
|
+
deleteOne: deleteOne,
|
|
679
|
+
deleteVersions: deleteVersions,
|
|
680
|
+
find: find,
|
|
681
|
+
findDistinct: ()=>Promise.reject(new Error('findDistinct is not yet implemented for Content API adapter')),
|
|
682
|
+
findGlobal: findGlobal,
|
|
683
|
+
findGlobalVersions: findGlobalVersions,
|
|
684
|
+
findOne: findOne,
|
|
685
|
+
findVersions: findVersions,
|
|
686
|
+
init,
|
|
687
|
+
packageName: '@payloadcms/db-content-api',
|
|
688
|
+
payload,
|
|
689
|
+
queryDrafts: queryDrafts,
|
|
690
|
+
rollbackTransaction: async ()=>{},
|
|
691
|
+
updateGlobal: updateGlobal,
|
|
692
|
+
updateGlobalVersion: updateGlobalVersion,
|
|
693
|
+
updateMany: updateMany,
|
|
694
|
+
updateOne: updateOne,
|
|
695
|
+
updateVersion: updateVersion,
|
|
696
|
+
upsert: upsert,
|
|
697
|
+
url: opts.url
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
});
|
|
701
|
+
|
|
702
|
+
//# sourceMappingURL=index.js.map
|