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