@payloadcms/figma 1.0.0-beta.1 → 1.0.0-beta.3
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/adapters/build-lambda-zip.d.ts +2 -1
- package/dist/adapters/build-lambda-zip.js +4 -1
- package/dist/adapters/deploy-output/assertDeployableServerActionsKey.d.ts +14 -0
- package/dist/adapters/deploy-output/assertDeployableServerActionsKey.js +33 -0
- package/dist/commands/upload-schema.js +23 -9
- package/dist/db-content-api/generated/content-api-types.d.ts +247 -0
- package/dist/db-content-api/index.js +87 -20
- package/dist/db-content-api/utilities/correlationIds.d.ts +18 -0
- package/dist/db-content-api/utilities/correlationIds.js +42 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/oauth/defaults.js +2 -2
- package/dist/oauth/hooks/me.d.ts +4 -3
- package/dist/oauth/hooks/me.js +6 -52
- package/dist/oauth/hooks/refresh.js +9 -1
- package/dist/oauth/index.js +1 -1
- package/dist/oauth/strategy/index.d.ts +3 -0
- package/dist/oauth/strategy/index.js +49 -3
- package/dist/oauth/utilities/clearCookie.d.ts +1 -0
- package/dist/oauth/utilities/clearCookie.js +13 -9
- package/dist/oauth/utilities/clearSessionCookies.d.ts +5 -0
- package/dist/oauth/utilities/clearSessionCookies.js +17 -0
- package/dist/oauth/utilities/getAuthorizeURL.js +1 -2
- package/dist/oauth/utilities/mergeHeaders.js +11 -23
- package/dist/oauth/utilities/refreshTokens.d.ts +9 -3
- package/dist/oauth/utilities/refreshTokens.js +45 -34
- package/dist/plugin/build-config.js +10 -7
- package/dist/plugin/dev-cookie-names.d.ts +4 -1
- package/dist/plugin/dev-cookie-names.js +6 -3
- package/dist/plugin/environment-error.d.ts +4 -0
- package/dist/plugin/environment-error.js +22 -0
- package/dist/plugin/mcp-oauth.d.ts +10 -0
- package/dist/plugin/mcp-oauth.js +24 -0
- package/dist/plugin/sandbox-upload-fetch.d.ts +5 -6
- package/dist/plugin/sandbox-upload-fetch.js +22 -14
- package/dist/scss.d.js +1 -1
- package/dist/storage-content-api/api-types.d.ts +22 -1
- package/dist/storage-content-api/api-types.js +2 -1
- package/dist/storage-content-api/client.d.ts +3 -3
- package/dist/storage-content-api/client.js +13 -35
- package/dist/storage-content-api/index.js +20 -21
- package/dist/storage-content-api/staticHandler.js +4 -1
- package/dist/storage-content-api/types.d.ts +2 -2
- package/dist/utils/env-management.js +7 -0
- package/package.json +21 -23
- package/dist/db-content-api/utilities/auth.d.ts +0 -29
- package/dist/db-content-api/utilities/auth.js +0 -82
package/dist/scss.d.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Side-effect SCSS imports in admin components carry styling only; declaring the
|
|
2
|
-
// module lets the type checker accept them while SWC/
|
|
2
|
+
// module lets the type checker accept them while SWC and scripts/copy-assets.mjs handle the assets.
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Upload API types extracted from Content API OpenAPI spec.
|
|
3
|
-
*
|
|
3
|
+
* Existing upload types stay separate to avoid breaking changes when the main spec is
|
|
4
|
+
* regenerated. The transform endpoint reuses its generated request/response schemas.
|
|
4
5
|
*/
|
|
6
|
+
import type { components } from '../db-content-api/generated/content-api-types.js';
|
|
7
|
+
type SignImageTransformRequest = components['schemas']['SignImageTransformRequest'];
|
|
5
8
|
type UploadErrorResponse = {
|
|
6
9
|
content: {
|
|
7
10
|
'application/json': {
|
|
@@ -182,5 +185,23 @@ export type UploadPaths = {
|
|
|
182
185
|
} & UploadErrorResponses;
|
|
183
186
|
};
|
|
184
187
|
};
|
|
188
|
+
'/api/v0/uploads/transform/sign': {
|
|
189
|
+
post: {
|
|
190
|
+
requestBody: {
|
|
191
|
+
content: {
|
|
192
|
+
'application/json': {
|
|
193
|
+
method?: SignImageTransformRequest['method'];
|
|
194
|
+
} & Omit<SignImageTransformRequest, 'method'>;
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
responses: {
|
|
198
|
+
200: {
|
|
199
|
+
content: {
|
|
200
|
+
'application/json': components['schemas']['SignImageTransformResponse'];
|
|
201
|
+
};
|
|
202
|
+
};
|
|
203
|
+
} & UploadErrorResponses;
|
|
204
|
+
};
|
|
205
|
+
};
|
|
185
206
|
};
|
|
186
207
|
export {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Upload API types extracted from Content API OpenAPI spec.
|
|
3
|
-
*
|
|
3
|
+
* Existing upload types stay separate to avoid breaking changes when the main spec is
|
|
4
|
+
* regenerated. The transform endpoint reuses its generated request/response schemas.
|
|
4
5
|
*/ export { };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import createClient from 'openapi-fetch';
|
|
2
1
|
import type { UploadPaths } from './api-types.js';
|
|
3
2
|
import type { StorageClientConfig } from './types.js';
|
|
4
|
-
|
|
5
|
-
export
|
|
3
|
+
import { createContentApiClient } from '../content-api/createContentApiClient.js';
|
|
4
|
+
export type StorageClient = ReturnType<typeof createContentApiClient<UploadPaths>>['api'];
|
|
5
|
+
export declare function createStorageClient(config: StorageClientConfig, warn?: (message: string) => void): StorageClient;
|
|
@@ -1,37 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
async onRequest ({ request }) {
|
|
14
|
-
if ('contentApiKey' in config) {
|
|
15
|
-
request.headers.set('X-Api-Key', config.contentApiKey);
|
|
16
|
-
} else {
|
|
17
|
-
const { auth, contentSystemId } = config;
|
|
18
|
-
if (auth.mode === 'apiKey') {
|
|
19
|
-
request.headers.set('X-Api-Key', auth.apiKey);
|
|
20
|
-
} else if (auth.mode === 'tokenStore') {
|
|
21
|
-
const token = await getValidProjectToken({
|
|
22
|
-
tenantId: contentSystemId,
|
|
23
|
-
tokenStore: auth.tokenStore
|
|
24
|
-
});
|
|
25
|
-
if (!token) {
|
|
26
|
-
throw new Error('Authentication required. Run `npx @payloadcms/figma login` to authenticate.');
|
|
27
|
-
}
|
|
28
|
-
request.headers.set('Authorization', `Bearer ${token}`);
|
|
29
|
-
} else {
|
|
30
|
-
const token = await fetchDevJwt(config.baseUrl, contentSystemId);
|
|
31
|
-
request.headers.set('Authorization', `Bearer ${token}`);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return request;
|
|
1
|
+
import { createContentApiClient } from '../content-api/createContentApiClient.js';
|
|
2
|
+
import { warning } from '../utils/log.js';
|
|
3
|
+
export function createStorageClient(config, warn = warning) {
|
|
4
|
+
return createContentApiClient({
|
|
5
|
+
auth: 'contentApiKey' in config ? {
|
|
6
|
+
apiKey: config.contentApiKey,
|
|
7
|
+
mode: 'apiKey'
|
|
8
|
+
} : config.auth,
|
|
9
|
+
baseUrl: config.baseUrl,
|
|
10
|
+
contentSystemId: 'contentSystemId' in config ? config.contentSystemId : '',
|
|
11
|
+
logger: {
|
|
12
|
+
warn
|
|
35
13
|
}
|
|
36
|
-
};
|
|
14
|
+
}).api;
|
|
37
15
|
}
|
|
@@ -7,37 +7,36 @@ import { getHandleUpload } from './handleUpload.js';
|
|
|
7
7
|
import { getHandler } from './staticHandler.js';
|
|
8
8
|
export function contentApiStorageAdapter(config) {
|
|
9
9
|
return ({ collection, prefix })=>{
|
|
10
|
-
const client = createStorageClient(config);
|
|
11
10
|
const baseUrl = config.baseUrl;
|
|
12
|
-
const deleteFile = getDeleteFile({
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
const deleteFile = (args)=>getDeleteFile({
|
|
12
|
+
client: createStorageClient(config, (message)=>args.req.payload.logger.warn(message))
|
|
13
|
+
})(args);
|
|
15
14
|
return {
|
|
16
15
|
name: 'content-api',
|
|
17
|
-
copyFile: getCopyFile({
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
copyFile: (args)=>getCopyFile({
|
|
17
|
+
client: createStorageClient(config, (message)=>args.req.payload.logger.warn(message))
|
|
18
|
+
})(args),
|
|
20
19
|
deleteFile,
|
|
21
20
|
generateURL: getGenerateURL({
|
|
22
21
|
baseUrl
|
|
23
22
|
}),
|
|
24
23
|
handleDelete: deleteFile,
|
|
25
|
-
handleUpload: getHandleUpload({
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
staticHandler: getHandler({
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
24
|
+
handleUpload: (args)=>getHandleUpload({
|
|
25
|
+
client: createStorageClient(config, (message)=>args.req.payload.logger.warn(message)),
|
|
26
|
+
collection
|
|
27
|
+
})(args),
|
|
28
|
+
staticHandler: (req, args)=>getHandler({
|
|
29
|
+
client: createStorageClient(config, (message)=>req.payload.logger.warn(message)),
|
|
30
|
+
collection,
|
|
31
|
+
prefix
|
|
32
|
+
})(req, args),
|
|
34
33
|
uploadInstructions: {
|
|
35
34
|
enabled: true,
|
|
36
|
-
generate: getGenerateUploadInstructions({
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
35
|
+
generate: (args)=>getGenerateUploadInstructions({
|
|
36
|
+
access: config.uploadInstructionsAccess,
|
|
37
|
+
client: createStorageClient(config, (message)=>args.req.payload.logger.warn(message)),
|
|
38
|
+
collectionPrefix: prefix
|
|
39
|
+
})(args),
|
|
41
40
|
requiresUploadReceipt: true,
|
|
42
41
|
useInAdmin: true
|
|
43
42
|
}
|
|
@@ -8,13 +8,16 @@ import { mediaErrorResponse } from './mediaErrorResponse.js';
|
|
|
8
8
|
import { resolveDownloadResponseHeaders } from './resolveDownloadResponseHeaders.js';
|
|
9
9
|
const PRESIGNED_DOWNLOAD_URL_EXPIRES_IN_SECONDS = 300;
|
|
10
10
|
export const getHandler = ({ client, collection, prefix = '' })=>{
|
|
11
|
-
return async (req, { doc, headers, params
|
|
11
|
+
return async (req, { doc, headers, params })=>{
|
|
12
|
+
const { filename, operation, uploadReference } = params;
|
|
13
|
+
const { isOriginal } = params;
|
|
12
14
|
try {
|
|
13
15
|
const prefixArgs = {
|
|
14
16
|
collection,
|
|
15
17
|
collectionPrefix: prefix,
|
|
16
18
|
doc,
|
|
17
19
|
filename,
|
|
20
|
+
isOriginal,
|
|
18
21
|
req,
|
|
19
22
|
uploadReference
|
|
20
23
|
};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { UploadInstructionsAccess } from 'payload';
|
|
2
|
-
import type {
|
|
2
|
+
import type { ContentApiAuth } from '../content-api/auth.js';
|
|
3
3
|
export type StorageClientConfig = {
|
|
4
4
|
uploadInstructionsAccess?: UploadInstructionsAccess;
|
|
5
5
|
} & ({
|
|
6
|
-
auth:
|
|
6
|
+
auth: ContentApiAuth;
|
|
7
7
|
baseUrl: string;
|
|
8
8
|
contentSystemId: string;
|
|
9
9
|
} | {
|
|
@@ -23,6 +23,11 @@ import path from 'path';
|
|
|
23
23
|
}
|
|
24
24
|
return null;
|
|
25
25
|
}
|
|
26
|
+
function assertEnvVarPartHasNoLineTerminatorsOrNullBytes(part, input) {
|
|
27
|
+
if (/[\r\n\u2028\u2029\0]/u.test(input)) {
|
|
28
|
+
throw new Error(`Environment variable ${part} must not contain line terminators or null bytes`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
26
31
|
/**
|
|
27
32
|
* Get an environment variable value from .env file (async)
|
|
28
33
|
*
|
|
@@ -70,6 +75,8 @@ import path from 'path';
|
|
|
70
75
|
* @param key - Environment variable name
|
|
71
76
|
* @param value - Environment variable value
|
|
72
77
|
*/ export async function addOrUpdateEnvVar(projectPath, key, value) {
|
|
78
|
+
assertEnvVarPartHasNoLineTerminatorsOrNullBytes('key', key);
|
|
79
|
+
assertEnvVarPartHasNoLineTerminatorsOrNullBytes('value', value);
|
|
73
80
|
const envPath = path.join(projectPath, '.env');
|
|
74
81
|
try {
|
|
75
82
|
let contents = '';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/figma",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.3",
|
|
4
4
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
],
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@clack/prompts": "^0.11.0",
|
|
41
|
-
"@cloudflare/workers-types": "5.
|
|
41
|
+
"@cloudflare/workers-types": "^5.20261108.1",
|
|
42
42
|
"@figma/rest-api-spec": "^0.34.0",
|
|
43
43
|
"@sindresorhus/slugify": "^3.0.0",
|
|
44
44
|
"archiver": "7.0.1",
|
|
@@ -58,41 +58,39 @@
|
|
|
58
58
|
"skills": "1.4.4",
|
|
59
59
|
"tar": "^7.5.21",
|
|
60
60
|
"terminal-link": "^5.0.0",
|
|
61
|
-
"ts-morph": "^21.0.1"
|
|
62
|
-
"uuid": "^10.0.0"
|
|
61
|
+
"ts-morph": "^21.0.1"
|
|
63
62
|
},
|
|
64
63
|
"devDependencies": {
|
|
65
64
|
"@payloadcms/eslint-config": "3.28.0",
|
|
66
65
|
"@payloadcms/eslint-plugin": "3.28.0",
|
|
67
|
-
"@payloadcms/next": "4.0.0-beta.
|
|
68
|
-
"@payloadcms/plugin-cloud-storage": "4.0.0-beta.
|
|
69
|
-
"@payloadcms/richtext-lexical": "4.0.0-beta.
|
|
70
|
-
"@payloadcms/translations": "4.0.0-beta.
|
|
71
|
-
"@payloadcms/ui": "4.0.0-beta.
|
|
66
|
+
"@payloadcms/next": "4.0.0-beta.9",
|
|
67
|
+
"@payloadcms/plugin-cloud-storage": "4.0.0-beta.9",
|
|
68
|
+
"@payloadcms/richtext-lexical": "4.0.0-beta.9",
|
|
69
|
+
"@payloadcms/translations": "4.0.0-beta.9",
|
|
70
|
+
"@payloadcms/ui": "4.0.0-beta.9",
|
|
72
71
|
"@swc/cli": "0.7.7",
|
|
73
72
|
"@types/archiver": "7.0.0",
|
|
74
73
|
"@types/cross-spawn": "6.0.6",
|
|
75
74
|
"@types/jsonwebtoken": "^9.0.10",
|
|
76
75
|
"@types/node": "^24.0.0",
|
|
77
76
|
"@types/react": "^19",
|
|
78
|
-
"@types/uuid": "^8.3.4",
|
|
79
|
-
"copyfiles": "^2.4.1",
|
|
80
77
|
"eslint": "9.22.0",
|
|
81
|
-
"next": "^16.
|
|
78
|
+
"next": "^16.4.2",
|
|
82
79
|
"openapi-typescript": "^7.13.0",
|
|
83
|
-
"payload": "4.0.0-beta.
|
|
84
|
-
"
|
|
80
|
+
"payload": "4.0.0-beta.9",
|
|
81
|
+
"react": "^19.3.1",
|
|
82
|
+
"react-dom": "^19.3.1",
|
|
85
83
|
"tsx": "4.20.6",
|
|
86
84
|
"typescript": "5.7.3",
|
|
87
85
|
"vitest": "4.0.15"
|
|
88
86
|
},
|
|
89
87
|
"peerDependencies": {
|
|
90
|
-
"@payloadcms/plugin-cloud-storage": ">=4.0.0-beta.
|
|
91
|
-
"@payloadcms/richtext-lexical": ">=4.0.0-beta.
|
|
92
|
-
"@payloadcms/translations": ">=4.0.0-beta.
|
|
93
|
-
"@payloadcms/ui": ">=4.0.0-beta.
|
|
94
|
-
"payload": ">=4.0.0-beta.
|
|
95
|
-
"react": "^19.
|
|
88
|
+
"@payloadcms/plugin-cloud-storage": ">=4.0.0-beta.9 <4.0.0-internal",
|
|
89
|
+
"@payloadcms/richtext-lexical": ">=4.0.0-beta.9 <4.0.0-internal",
|
|
90
|
+
"@payloadcms/translations": ">=4.0.0-beta.9 <4.0.0-internal",
|
|
91
|
+
"@payloadcms/ui": ">=4.0.0-beta.9 <4.0.0-internal",
|
|
92
|
+
"payload": ">=4.0.0-beta.9 <4.0.0-internal",
|
|
93
|
+
"react": "^19.3.1"
|
|
96
94
|
},
|
|
97
95
|
"peerDependenciesMeta": {
|
|
98
96
|
"@payloadcms/plugin-cloud-storage": {
|
|
@@ -106,11 +104,11 @@
|
|
|
106
104
|
"registry": "https://registry.npmjs.org/"
|
|
107
105
|
},
|
|
108
106
|
"scripts": {
|
|
109
|
-
"build": "pnpm build:swc && pnpm build:types && pnpm
|
|
107
|
+
"build": "pnpm build:swc && pnpm build:types && pnpm build:assets",
|
|
108
|
+
"build:assets": "node scripts/copy-assets.mjs",
|
|
110
109
|
"build:swc": "swc ./src -d ./dist --config-file .swcrc --strip-leading-paths",
|
|
111
110
|
"build:types": "tsc --project tsconfig.build.json",
|
|
112
|
-
"clean": "
|
|
113
|
-
"copyfiles": "copyfiles -u 1 \"src/**/*.{html,css,scss,ttf,woff,woff2,eot,svg,jpg,png,md}\" \"src/**/.env.example\" dist/",
|
|
111
|
+
"clean": "rm -rf dist *.tsbuildinfo package",
|
|
114
112
|
"debug:clear-tokens": "tsx scripts/clear-tokens.ts",
|
|
115
113
|
"debug:expire-token": "tsx scripts/expire-token.ts",
|
|
116
114
|
"debug:get-project-token": "tsx scripts/get-project-token.ts",
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { Middleware } from 'openapi-fetch';
|
|
2
|
-
import type { Payload } from 'payload';
|
|
3
|
-
import type { TokenStore } from '../../auth/token-store.js';
|
|
4
|
-
type ApiKeyAuth = {
|
|
5
|
-
apiKey: string;
|
|
6
|
-
mode: 'apiKey';
|
|
7
|
-
};
|
|
8
|
-
type TokenStoreAuth = {
|
|
9
|
-
mode: 'tokenStore';
|
|
10
|
-
tokenStore: TokenStore;
|
|
11
|
-
};
|
|
12
|
-
type DevJwtAuth = {
|
|
13
|
-
mode: 'devJwt';
|
|
14
|
-
};
|
|
15
|
-
export type AuthMode = ApiKeyAuth | DevJwtAuth | TokenStoreAuth;
|
|
16
|
-
export declare function fetchDevJwt(url: string, contentSystemId: string): Promise<string>;
|
|
17
|
-
export declare function createAuthMiddleware(opts: {
|
|
18
|
-
auth: AuthMode;
|
|
19
|
-
contentSystemId: string;
|
|
20
|
-
logger: Payload['logger'];
|
|
21
|
-
url: string;
|
|
22
|
-
}): Middleware;
|
|
23
|
-
export declare function createErrorMiddleware(opts: {
|
|
24
|
-
auth: AuthMode;
|
|
25
|
-
contentSystemId: string;
|
|
26
|
-
logger: Payload['logger'];
|
|
27
|
-
url: string;
|
|
28
|
-
}): Middleware;
|
|
29
|
-
export {};
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { getValidProjectToken } from '../../auth/project-token.js';
|
|
2
|
-
// Module-level cache for dev JWT tokens
|
|
3
|
-
const devJwtCache = new Map();
|
|
4
|
-
export async function fetchDevJwt(url, contentSystemId) {
|
|
5
|
-
const usesSuperUser = !contentSystemId || contentSystemId === 'test-system' || contentSystemId === '*';
|
|
6
|
-
const contentSystemIdForJWT = usesSuperUser ? '*' : contentSystemId;
|
|
7
|
-
const response = await fetch(`${url}/dev/jwt`, {
|
|
8
|
-
body: JSON.stringify({
|
|
9
|
-
content_system_id: contentSystemIdForJWT
|
|
10
|
-
}),
|
|
11
|
-
headers: {
|
|
12
|
-
'Content-Type': 'application/json'
|
|
13
|
-
},
|
|
14
|
-
method: 'POST'
|
|
15
|
-
});
|
|
16
|
-
if (!response.ok) {
|
|
17
|
-
throw new Error(`Failed to get dev JWT: ${response.status} ${response.statusText}`);
|
|
18
|
-
}
|
|
19
|
-
const { token } = await response.json();
|
|
20
|
-
return token;
|
|
21
|
-
}
|
|
22
|
-
export function createAuthMiddleware(opts) {
|
|
23
|
-
return {
|
|
24
|
-
async onRequest ({ request }) {
|
|
25
|
-
if (opts.auth.mode === 'apiKey') {
|
|
26
|
-
request.headers.set('X-Api-Key', opts.auth.apiKey);
|
|
27
|
-
} else if (opts.auth.mode === 'tokenStore') {
|
|
28
|
-
const token = await getValidProjectToken({
|
|
29
|
-
tenantId: opts.contentSystemId,
|
|
30
|
-
tokenStore: opts.auth.tokenStore
|
|
31
|
-
});
|
|
32
|
-
if (!token) {
|
|
33
|
-
throw new Error('Authentication required. Run `npx @payloadcms/figma login` to authenticate.');
|
|
34
|
-
}
|
|
35
|
-
request.headers.set('Authorization', `Bearer ${token}`);
|
|
36
|
-
} else {
|
|
37
|
-
let token = devJwtCache.get(opts.contentSystemId);
|
|
38
|
-
if (!token) {
|
|
39
|
-
token = await fetchDevJwt(opts.url, opts.contentSystemId);
|
|
40
|
-
devJwtCache.set(opts.contentSystemId, token);
|
|
41
|
-
opts.logger.info(`Dev JWT acquired for content system: ${opts.contentSystemId}`);
|
|
42
|
-
}
|
|
43
|
-
request.headers.set('Authorization', `Bearer ${token}`);
|
|
44
|
-
}
|
|
45
|
-
return request;
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
export function createErrorMiddleware(opts) {
|
|
50
|
-
return {
|
|
51
|
-
async onResponse ({ request, response }) {
|
|
52
|
-
if (!response.ok) {
|
|
53
|
-
// Handle 401 retry for dev JWT
|
|
54
|
-
if (response.status === 401 && opts.auth.mode === 'devJwt') {
|
|
55
|
-
const retryCount = parseInt(request.headers.get('X-Retry-Count') || '0', 10);
|
|
56
|
-
if (retryCount < 1) {
|
|
57
|
-
opts.logger.info('Dev JWT expired, refreshing...');
|
|
58
|
-
devJwtCache.delete(opts.contentSystemId);
|
|
59
|
-
const newToken = await fetchDevJwt(opts.url, opts.contentSystemId);
|
|
60
|
-
devJwtCache.set(opts.contentSystemId, newToken);
|
|
61
|
-
// duplex: 'half' is required for Node.js fetch with streaming body
|
|
62
|
-
const newRequest = new Request(request.url, {
|
|
63
|
-
body: request.body,
|
|
64
|
-
duplex: 'half',
|
|
65
|
-
headers: new Headers(request.headers),
|
|
66
|
-
method: request.method
|
|
67
|
-
});
|
|
68
|
-
newRequest.headers.set('X-Retry-Count', '1');
|
|
69
|
-
newRequest.headers.set('Authorization', `Bearer ${newToken}`);
|
|
70
|
-
return fetch(newRequest);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
const text = await response.clone().text();
|
|
74
|
-
opts.logger.error({
|
|
75
|
-
msg: `HTTP ${response.status} from ${request.url}`,
|
|
76
|
-
response: text
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
return response;
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
}
|