@storyblok/management-api-client 0.1.8 → 0.1.9
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/client/client.js +6 -1
- package/dist/client/client.js.map +1 -1
- package/dist/client/client.mjs +6 -1
- package/dist/client/client.mjs.map +1 -1
- package/dist/client/error.d.mts +19 -0
- package/dist/client/error.d.ts +19 -0
- package/dist/client/error.js +24 -0
- package/dist/client/error.js.map +1 -0
- package/dist/client/error.mjs +23 -0
- package/dist/client/error.mjs.map +1 -0
- package/dist/client/index.js +1 -0
- package/dist/client/index.mjs +1 -0
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/client/client.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.js');
|
|
2
|
+
const require_error = require('./error.js');
|
|
2
3
|
const require_utils = require('./utils.js');
|
|
3
4
|
const require_calculate_retry_delay = require('../utils/calculate-retry-delay.js');
|
|
4
5
|
const require_delay = require('../utils/delay.js');
|
|
@@ -86,7 +87,11 @@ const createClient = (config) => {
|
|
|
86
87
|
let finalError = error;
|
|
87
88
|
for (const fn of interceptors.error._fns) if (fn) finalError = await fn(error, response, request$1, opts);
|
|
88
89
|
finalError = finalError || {};
|
|
89
|
-
if (opts.throwOnError) throw
|
|
90
|
+
if (opts.throwOnError) throw new require_error.ClientError(response.statusText || "API request failed", {
|
|
91
|
+
status: response.status,
|
|
92
|
+
statusText: response.statusText,
|
|
93
|
+
data: finalError
|
|
94
|
+
});
|
|
90
95
|
return opts.responseStyle === "data" ? void 0 : {
|
|
91
96
|
error: finalError,
|
|
92
97
|
...result
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","names":["config: Config","mergeConfigs","createConfig","config","createInterceptors","request: Client['request']","mergeHeaders","setAuthParams","buildUrl","requestInit: ReqInit","request","getParseAs","data: any","jsonError: unknown","fetchFn: any","url: string","retryConfig: { maxRetries: number; retryDelay: number }","attempt: number","calculateRetryDelay","delay"],"sources":["../../src/client/client.ts"],"sourcesContent":["import { getManagementBaseUrl, getRegion } from '@storyblok/region-helper';\nimport type { Client, Config, RequestOptions } from './types';\nimport {\n buildUrl,\n createConfig,\n createInterceptors,\n getParseAs,\n mergeConfigs,\n mergeHeaders,\n setAuthParams,\n} from './utils';\nimport { calculateRetryDelay } from \"../utils/calculate-retry-delay\";\nimport { delay } from \"../utils/delay\";\n\ntype ReqInit = Omit<RequestInit, 'body' | 'headers'> & {\n body?: any;\n headers: ReturnType<typeof mergeHeaders>;\n};\n\nexport const createClient = (config: Config): Client => {\n let _config = mergeConfigs(createConfig(), config);\n\n const getConfig = (): Config => ({ ..._config });\n\n const setConfig = (config: Config): Config => {\n _config = mergeConfigs(_config, config);\n return getConfig();\n };\n\n const interceptors = createInterceptors<\n Request,\n Response,\n unknown,\n RequestOptions\n >();\n\n const request: Client['request'] = async (options) => {\n const opts = {\n ..._config,\n ...options,\n fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,\n headers: mergeHeaders(_config.headers, options.headers),\n };\n\n // If the baseUrl is not set and we have a space_id, we can attempt to infer the region\n if (!_config.baseUrl && options.path?.space_id) {\n const region = getRegion(options.path.space_id as number, opts.region);\n if (region) {\n opts.baseUrl = getManagementBaseUrl(region, 'https');\n }\n } else if (!_config.baseUrl && opts.region) {\n opts.baseUrl = getManagementBaseUrl(opts.region, 'https');\n }\n\n if (opts.security) {\n await setAuthParams({\n ...opts,\n security: opts.security,\n });\n }\n\n if (opts.requestValidator) {\n await opts.requestValidator(opts);\n }\n\n if (opts.body && opts.bodySerializer) {\n opts.body = opts.bodySerializer(opts.body);\n }\n\n // remove Content-Type header if body is empty to avoid sending invalid requests\n if (opts.body === undefined || opts.body === '') {\n opts.headers.delete('Content-Type');\n }\n\n const url = buildUrl(opts);\n const requestInit: ReqInit = {\n redirect: 'follow',\n ...opts,\n };\n\n let request = new Request(url, requestInit);\n\n for (const fn of interceptors.request._fns) {\n if (fn) {\n request = await fn(request, opts);\n }\n }\n\n // fetch must be assigned here, otherwise it would throw the error:\n // TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation\n const _fetch = opts.fetch!;\n \n // Execute with retry logic by recreating the request for each attempt\n let response = await executeWithRetry(_fetch, url, requestInit, {\n maxRetries: 12,\n retryDelay: 1000\n });\n\n for (const fn of interceptors.response._fns) {\n if (fn) {\n response = await fn(response, request, opts);\n }\n }\n\n const result = {\n request,\n response,\n };\n\n if (response.ok) {\n if (\n response.status === 204 ||\n response.headers.get('Content-Length') === '0'\n ) {\n return opts.responseStyle === 'data'\n ? {}\n : {\n data: {},\n ...result,\n };\n }\n\n const parseAs =\n (opts.parseAs === 'auto'\n ? getParseAs(response.headers.get('Content-Type'))\n : opts.parseAs) ?? 'json';\n\n let data: any;\n switch (parseAs) {\n case 'arrayBuffer':\n case 'blob':\n case 'formData':\n case 'json':\n case 'text':\n data = await response[parseAs]();\n break;\n case 'stream':\n return opts.responseStyle === 'data'\n ? response.body\n : {\n data: response.body,\n ...result,\n };\n }\n\n if (parseAs === 'json') {\n if (opts.responseValidator) {\n await opts.responseValidator(data);\n }\n\n if (opts.responseTransformer) {\n data = await opts.responseTransformer(data);\n }\n }\n\n return opts.responseStyle === 'data'\n ? data\n : {\n data,\n ...result,\n };\n }\n\n const textError = await response.text();\n let jsonError: unknown;\n\n try {\n jsonError = JSON.parse(textError);\n } catch {\n // noop\n }\n\n const error = jsonError ?? textError;\n let finalError = error;\n\n for (const fn of interceptors.error._fns) {\n if (fn) {\n finalError = (await fn(error, response, request, opts)) as string;\n }\n }\n\n finalError = finalError || ({} as string);\n\n if (opts.throwOnError) {\n throw finalError;\n }\n\n // TODO: we probably want to return error and improve types\n return opts.responseStyle === 'data'\n ? undefined\n : {\n error: finalError,\n ...result,\n };\n };\n\n // Helper function to execute fetch with retry logic\n async function executeWithRetry(\n fetchFn: any,\n url: string,\n requestInit: ReqInit,\n retryConfig: { maxRetries: number; retryDelay: number },\n attempt: number = 0\n ): Promise<Response> {\n try {\n const request = new Request(url, requestInit);\n const response = await fetchFn(request);\n \n if (response.status === 429 && attempt < retryConfig.maxRetries) {\n const retryAfter = response.headers.get('retry-after');\n const retryDelay = retryAfter ? parseInt(retryAfter) * 1000 : calculateRetryDelay(attempt, retryConfig.retryDelay);\n await delay(retryDelay);\n \n // Use the original unconsumed request for retry\n return executeWithRetry(fetchFn, url, requestInit, retryConfig, attempt + 1);\n }\n \n return response;\n } catch (error) {\n // If it's a network error and we haven't exceeded retries, try again\n if (attempt < retryConfig.maxRetries) {\n const delay = retryConfig.retryDelay;\n await new Promise(resolve => setTimeout(resolve, delay));\n \n // Use the original unconsumed request for retry\n return executeWithRetry(fetchFn, url, requestInit, retryConfig, attempt + 1);\n }\n \n throw error;\n }\n }\n\n return {\n buildUrl,\n connect: (options) => request({ ...options, method: 'CONNECT' }),\n delete: (options) => request({ ...options, method: 'DELETE' }),\n get: (options) => request({ ...options, method: 'GET' }),\n getConfig,\n head: (options) => request({ ...options, method: 'HEAD' }),\n interceptors,\n options: (options) => request({ ...options, method: 'OPTIONS' }),\n patch: (options) => request({ ...options, method: 'PATCH' }),\n post: (options) => request({ ...options, method: 'POST' }),\n put: (options) => request({ ...options, method: 'PUT' }),\n request,\n setConfig,\n trace: (options) => request({ ...options, method: 'TRACE' }),\n };\n};\n"],"mappings":";;;;;;;AAmBA,MAAa,eAAe,CAACA,WAA2B;CACtD,IAAI,UAAUC,2BAAaC,4BAAc,EAAE,OAAO;CAElD,MAAM,YAAY,OAAe,EAAE,GAAG,QAAS;CAE/C,MAAM,YAAY,CAACF,aAA2B;EAC5C,UAAUC,2BAAa,SAASE,SAAO;AACvC,SAAO,WAAW;CACnB;CAED,MAAM,eAAeC,kCAKlB;CAEH,MAAMC,UAA6B,OAAO,YAAY;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H,MAAMC,oBAAM,WAAW;AAGvB,WAAO,iBAAiB,SAAS,KAAK,aAAa,aAAa,UAAU,EAAE;GAC7E;AAED,UAAO;EACR,SAAQ,OAAO;AAEd,OAAI,UAAU,YAAY,YAAY;IACpC,MAAMA,UAAQ,YAAY;IAC1B,MAAM,IAAI,QAAQ,aAAW,WAAW,SAASA,QAAM;AAGvD,WAAO,iBAAiB,SAAS,KAAK,aAAa,aAAa,UAAU,EAAE;GAC7E;AAED,SAAM;EACP;CACF;AAED,QAAO;EACL;EACA,SAAS,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAW,EAAC;EAChE,QAAQ,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAU,EAAC;EAC9D,KAAK,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAO,EAAC;EACxD;EACA,MAAM,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAQ,EAAC;EAC1D;EACA,SAAS,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAW,EAAC;EAChE,OAAO,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAS,EAAC;EAC5D,MAAM,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAQ,EAAC;EAC1D,KAAK,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAO,EAAC;EACxD;EACA;EACA,OAAO,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAS,EAAC;CAC7D;AACF"}
|
|
1
|
+
{"version":3,"file":"client.js","names":["config: Config","mergeConfigs","createConfig","config","createInterceptors","request: Client['request']","mergeHeaders","setAuthParams","buildUrl","requestInit: ReqInit","request","getParseAs","data: any","jsonError: unknown","ClientError","fetchFn: any","url: string","retryConfig: { maxRetries: number; retryDelay: number }","attempt: number","calculateRetryDelay","delay"],"sources":["../../src/client/client.ts"],"sourcesContent":["import { getManagementBaseUrl, getRegion } from '@storyblok/region-helper';\nimport type { Client, Config, RequestOptions } from './types';\nimport { ClientError } from './error';\nimport {\n buildUrl,\n createConfig,\n createInterceptors,\n getParseAs,\n mergeConfigs,\n mergeHeaders,\n setAuthParams,\n} from './utils';\nimport { calculateRetryDelay } from \"../utils/calculate-retry-delay\";\nimport { delay } from \"../utils/delay\";\n\ntype ReqInit = Omit<RequestInit, 'body' | 'headers'> & {\n body?: any;\n headers: ReturnType<typeof mergeHeaders>;\n};\n\nexport const createClient = (config: Config): Client => {\n let _config = mergeConfigs(createConfig(), config);\n\n const getConfig = (): Config => ({ ..._config });\n\n const setConfig = (config: Config): Config => {\n _config = mergeConfigs(_config, config);\n return getConfig();\n };\n\n const interceptors = createInterceptors<\n Request,\n Response,\n unknown,\n RequestOptions\n >();\n\n const request: Client['request'] = async (options) => {\n const opts = {\n ..._config,\n ...options,\n fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,\n headers: mergeHeaders(_config.headers, options.headers),\n };\n\n // If the baseUrl is not set and we have a space_id, we can attempt to infer the region\n if (!_config.baseUrl && options.path?.space_id) {\n const region = getRegion(options.path.space_id as number, opts.region);\n if (region) {\n opts.baseUrl = getManagementBaseUrl(region, 'https');\n }\n } else if (!_config.baseUrl && opts.region) {\n opts.baseUrl = getManagementBaseUrl(opts.region, 'https');\n }\n\n if (opts.security) {\n await setAuthParams({\n ...opts,\n security: opts.security,\n });\n }\n\n if (opts.requestValidator) {\n await opts.requestValidator(opts);\n }\n\n if (opts.body && opts.bodySerializer) {\n opts.body = opts.bodySerializer(opts.body);\n }\n\n // remove Content-Type header if body is empty to avoid sending invalid requests\n if (opts.body === undefined || opts.body === '') {\n opts.headers.delete('Content-Type');\n }\n\n const url = buildUrl(opts);\n const requestInit: ReqInit = {\n redirect: 'follow',\n ...opts,\n };\n\n let request = new Request(url, requestInit);\n\n for (const fn of interceptors.request._fns) {\n if (fn) {\n request = await fn(request, opts);\n }\n }\n\n // fetch must be assigned here, otherwise it would throw the error:\n // TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation\n const _fetch = opts.fetch!;\n \n // Execute with retry logic by recreating the request for each attempt\n let response = await executeWithRetry(_fetch, url, requestInit, {\n maxRetries: 12,\n retryDelay: 1000\n });\n\n for (const fn of interceptors.response._fns) {\n if (fn) {\n response = await fn(response, request, opts);\n }\n }\n\n const result = {\n request,\n response,\n };\n\n if (response.ok) {\n if (\n response.status === 204 ||\n response.headers.get('Content-Length') === '0'\n ) {\n return opts.responseStyle === 'data'\n ? {}\n : {\n data: {},\n ...result,\n };\n }\n\n const parseAs =\n (opts.parseAs === 'auto'\n ? getParseAs(response.headers.get('Content-Type'))\n : opts.parseAs) ?? 'json';\n\n let data: any;\n switch (parseAs) {\n case 'arrayBuffer':\n case 'blob':\n case 'formData':\n case 'json':\n case 'text':\n data = await response[parseAs]();\n break;\n case 'stream':\n return opts.responseStyle === 'data'\n ? response.body\n : {\n data: response.body,\n ...result,\n };\n }\n\n if (parseAs === 'json') {\n if (opts.responseValidator) {\n await opts.responseValidator(data);\n }\n\n if (opts.responseTransformer) {\n data = await opts.responseTransformer(data);\n }\n }\n\n return opts.responseStyle === 'data'\n ? data\n : {\n data,\n ...result,\n };\n }\n\n const textError = await response.text();\n let jsonError: unknown;\n\n try {\n jsonError = JSON.parse(textError);\n } catch {\n // noop\n }\n\n const error = jsonError ?? textError;\n let finalError = error;\n\n for (const fn of interceptors.error._fns) {\n if (fn) {\n finalError = (await fn(error, response, request, opts)) as string;\n }\n }\n\n finalError = finalError || ({} as string);\n\n if (opts.throwOnError) {\n throw new ClientError(response.statusText || 'API request failed', {\n status: response.status,\n statusText: response.statusText,\n data: finalError,\n });\n }\n\n // TODO: we probably want to return error and improve types\n return opts.responseStyle === 'data'\n ? undefined\n : {\n error: finalError,\n ...result,\n };\n };\n\n // Helper function to execute fetch with retry logic\n async function executeWithRetry(\n fetchFn: any,\n url: string,\n requestInit: ReqInit,\n retryConfig: { maxRetries: number; retryDelay: number },\n attempt: number = 0\n ): Promise<Response> {\n try {\n const request = new Request(url, requestInit);\n const response = await fetchFn(request);\n \n if (response.status === 429 && attempt < retryConfig.maxRetries) {\n const retryAfter = response.headers.get('retry-after');\n const retryDelay = retryAfter ? parseInt(retryAfter) * 1000 : calculateRetryDelay(attempt, retryConfig.retryDelay);\n await delay(retryDelay);\n \n // Use the original unconsumed request for retry\n return executeWithRetry(fetchFn, url, requestInit, retryConfig, attempt + 1);\n }\n \n return response;\n } catch (error) {\n // If it's a network error and we haven't exceeded retries, try again\n if (attempt < retryConfig.maxRetries) {\n const delay = retryConfig.retryDelay;\n await new Promise(resolve => setTimeout(resolve, delay));\n \n // Use the original unconsumed request for retry\n return executeWithRetry(fetchFn, url, requestInit, retryConfig, attempt + 1);\n }\n \n throw error;\n }\n }\n\n return {\n buildUrl,\n connect: (options) => request({ ...options, method: 'CONNECT' }),\n delete: (options) => request({ ...options, method: 'DELETE' }),\n get: (options) => request({ ...options, method: 'GET' }),\n getConfig,\n head: (options) => request({ ...options, method: 'HEAD' }),\n interceptors,\n options: (options) => request({ ...options, method: 'OPTIONS' }),\n patch: (options) => request({ ...options, method: 'PATCH' }),\n post: (options) => request({ ...options, method: 'POST' }),\n put: (options) => request({ ...options, method: 'PUT' }),\n request,\n setConfig,\n trace: (options) => request({ ...options, method: 'TRACE' }),\n };\n};\n"],"mappings":";;;;;;;;AAoBA,MAAa,eAAe,CAACA,WAA2B;CACtD,IAAI,UAAUC,2BAAaC,4BAAc,EAAE,OAAO;CAElD,MAAM,YAAY,OAAe,EAAE,GAAG,QAAS;CAE/C,MAAM,YAAY,CAACF,aAA2B;EAC5C,UAAUC,2BAAa,SAASE,SAAO;AACvC,SAAO,WAAW;CACnB;CAED,MAAM,eAAeC,kCAKlB;CAEH,MAAMC,UAA6B,OAAO,YAAY;EACpD,MAAM,OAAO;GACX,GAAG;GACH,GAAG;GACH,OAAO,QAAQ,SAAS,QAAQ,SAAS,WAAW;GACpD,SAASC,2BAAa,QAAQ,S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AAGvB,WAAO,iBAAiB,SAAS,KAAK,aAAa,aAAa,UAAU,EAAE;GAC7E;AAED,UAAO;EACR,SAAQ,OAAO;AAEd,OAAI,UAAU,YAAY,YAAY;IACpC,MAAMA,UAAQ,YAAY;IAC1B,MAAM,IAAI,QAAQ,aAAW,WAAW,SAASA,QAAM;AAGvD,WAAO,iBAAiB,SAAS,KAAK,aAAa,aAAa,UAAU,EAAE;GAC7E;AAED,SAAM;EACP;CACF;AAED,QAAO;EACL;EACA,SAAS,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAW,EAAC;EAChE,QAAQ,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAU,EAAC;EAC9D,KAAK,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAO,EAAC;EACxD;EACA,MAAM,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAQ,EAAC;EAC1D;EACA,SAAS,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAW,EAAC;EAChE,OAAO,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAS,EAAC;EAC5D,MAAM,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAQ,EAAC;EAC1D,KAAK,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAO,EAAC;EACxD;EACA;EACA,OAAO,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAS,EAAC;CAC7D;AACF"}
|
package/dist/client/client.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ClientError } from "./error.mjs";
|
|
1
2
|
import { buildUrl, createConfig, createInterceptors, getParseAs, mergeConfigs, mergeHeaders, setAuthParams } from "./utils.mjs";
|
|
2
3
|
import { calculateRetryDelay } from "../utils/calculate-retry-delay.mjs";
|
|
3
4
|
import { delay } from "../utils/delay.mjs";
|
|
@@ -85,7 +86,11 @@ const createClient = (config) => {
|
|
|
85
86
|
let finalError = error;
|
|
86
87
|
for (const fn of interceptors.error._fns) if (fn) finalError = await fn(error, response, request$1, opts);
|
|
87
88
|
finalError = finalError || {};
|
|
88
|
-
if (opts.throwOnError) throw
|
|
89
|
+
if (opts.throwOnError) throw new ClientError(response.statusText || "API request failed", {
|
|
90
|
+
status: response.status,
|
|
91
|
+
statusText: response.statusText,
|
|
92
|
+
data: finalError
|
|
93
|
+
});
|
|
89
94
|
return opts.responseStyle === "data" ? void 0 : {
|
|
90
95
|
error: finalError,
|
|
91
96
|
...result
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.mjs","names":["config: Config","config","request: Client['request']","requestInit: ReqInit","request","data: any","jsonError: unknown","fetchFn: any","url: string","retryConfig: { maxRetries: number; retryDelay: number }","attempt: number","delay"],"sources":["../../src/client/client.ts"],"sourcesContent":["import { getManagementBaseUrl, getRegion } from '@storyblok/region-helper';\nimport type { Client, Config, RequestOptions } from './types';\nimport {\n buildUrl,\n createConfig,\n createInterceptors,\n getParseAs,\n mergeConfigs,\n mergeHeaders,\n setAuthParams,\n} from './utils';\nimport { calculateRetryDelay } from \"../utils/calculate-retry-delay\";\nimport { delay } from \"../utils/delay\";\n\ntype ReqInit = Omit<RequestInit, 'body' | 'headers'> & {\n body?: any;\n headers: ReturnType<typeof mergeHeaders>;\n};\n\nexport const createClient = (config: Config): Client => {\n let _config = mergeConfigs(createConfig(), config);\n\n const getConfig = (): Config => ({ ..._config });\n\n const setConfig = (config: Config): Config => {\n _config = mergeConfigs(_config, config);\n return getConfig();\n };\n\n const interceptors = createInterceptors<\n Request,\n Response,\n unknown,\n RequestOptions\n >();\n\n const request: Client['request'] = async (options) => {\n const opts = {\n ..._config,\n ...options,\n fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,\n headers: mergeHeaders(_config.headers, options.headers),\n };\n\n // If the baseUrl is not set and we have a space_id, we can attempt to infer the region\n if (!_config.baseUrl && options.path?.space_id) {\n const region = getRegion(options.path.space_id as number, opts.region);\n if (region) {\n opts.baseUrl = getManagementBaseUrl(region, 'https');\n }\n } else if (!_config.baseUrl && opts.region) {\n opts.baseUrl = getManagementBaseUrl(opts.region, 'https');\n }\n\n if (opts.security) {\n await setAuthParams({\n ...opts,\n security: opts.security,\n });\n }\n\n if (opts.requestValidator) {\n await opts.requestValidator(opts);\n }\n\n if (opts.body && opts.bodySerializer) {\n opts.body = opts.bodySerializer(opts.body);\n }\n\n // remove Content-Type header if body is empty to avoid sending invalid requests\n if (opts.body === undefined || opts.body === '') {\n opts.headers.delete('Content-Type');\n }\n\n const url = buildUrl(opts);\n const requestInit: ReqInit = {\n redirect: 'follow',\n ...opts,\n };\n\n let request = new Request(url, requestInit);\n\n for (const fn of interceptors.request._fns) {\n if (fn) {\n request = await fn(request, opts);\n }\n }\n\n // fetch must be assigned here, otherwise it would throw the error:\n // TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation\n const _fetch = opts.fetch!;\n \n // Execute with retry logic by recreating the request for each attempt\n let response = await executeWithRetry(_fetch, url, requestInit, {\n maxRetries: 12,\n retryDelay: 1000\n });\n\n for (const fn of interceptors.response._fns) {\n if (fn) {\n response = await fn(response, request, opts);\n }\n }\n\n const result = {\n request,\n response,\n };\n\n if (response.ok) {\n if (\n response.status === 204 ||\n response.headers.get('Content-Length') === '0'\n ) {\n return opts.responseStyle === 'data'\n ? {}\n : {\n data: {},\n ...result,\n };\n }\n\n const parseAs =\n (opts.parseAs === 'auto'\n ? getParseAs(response.headers.get('Content-Type'))\n : opts.parseAs) ?? 'json';\n\n let data: any;\n switch (parseAs) {\n case 'arrayBuffer':\n case 'blob':\n case 'formData':\n case 'json':\n case 'text':\n data = await response[parseAs]();\n break;\n case 'stream':\n return opts.responseStyle === 'data'\n ? response.body\n : {\n data: response.body,\n ...result,\n };\n }\n\n if (parseAs === 'json') {\n if (opts.responseValidator) {\n await opts.responseValidator(data);\n }\n\n if (opts.responseTransformer) {\n data = await opts.responseTransformer(data);\n }\n }\n\n return opts.responseStyle === 'data'\n ? data\n : {\n data,\n ...result,\n };\n }\n\n const textError = await response.text();\n let jsonError: unknown;\n\n try {\n jsonError = JSON.parse(textError);\n } catch {\n // noop\n }\n\n const error = jsonError ?? textError;\n let finalError = error;\n\n for (const fn of interceptors.error._fns) {\n if (fn) {\n finalError = (await fn(error, response, request, opts)) as string;\n }\n }\n\n finalError = finalError || ({} as string);\n\n if (opts.throwOnError) {\n throw finalError;\n }\n\n // TODO: we probably want to return error and improve types\n return opts.responseStyle === 'data'\n ? undefined\n : {\n error: finalError,\n ...result,\n };\n };\n\n // Helper function to execute fetch with retry logic\n async function executeWithRetry(\n fetchFn: any,\n url: string,\n requestInit: ReqInit,\n retryConfig: { maxRetries: number; retryDelay: number },\n attempt: number = 0\n ): Promise<Response> {\n try {\n const request = new Request(url, requestInit);\n const response = await fetchFn(request);\n \n if (response.status === 429 && attempt < retryConfig.maxRetries) {\n const retryAfter = response.headers.get('retry-after');\n const retryDelay = retryAfter ? parseInt(retryAfter) * 1000 : calculateRetryDelay(attempt, retryConfig.retryDelay);\n await delay(retryDelay);\n \n // Use the original unconsumed request for retry\n return executeWithRetry(fetchFn, url, requestInit, retryConfig, attempt + 1);\n }\n \n return response;\n } catch (error) {\n // If it's a network error and we haven't exceeded retries, try again\n if (attempt < retryConfig.maxRetries) {\n const delay = retryConfig.retryDelay;\n await new Promise(resolve => setTimeout(resolve, delay));\n \n // Use the original unconsumed request for retry\n return executeWithRetry(fetchFn, url, requestInit, retryConfig, attempt + 1);\n }\n \n throw error;\n }\n }\n\n return {\n buildUrl,\n connect: (options) => request({ ...options, method: 'CONNECT' }),\n delete: (options) => request({ ...options, method: 'DELETE' }),\n get: (options) => request({ ...options, method: 'GET' }),\n getConfig,\n head: (options) => request({ ...options, method: 'HEAD' }),\n interceptors,\n options: (options) => request({ ...options, method: 'OPTIONS' }),\n patch: (options) => request({ ...options, method: 'PATCH' }),\n post: (options) => request({ ...options, method: 'POST' }),\n put: (options) => request({ ...options, method: 'PUT' }),\n request,\n setConfig,\n trace: (options) => request({ ...options, method: 'TRACE' }),\n };\n};\n"],"mappings":";;;;;;AAmBA,MAAa,eAAe,CAACA,WAA2B;CACtD,IAAI,UAAU,aAAa,cAAc,EAAE,OAAO;CAElD,MAAM,YAAY,OAAe,EAAE,GAAG,QAAS;CAE/C,MAAM,YAAY,CAACA,aAA2B;EAC5C,UAAU,aAAa,SAASC,SAAO;AACvC,SAAO,WAAW;CACnB;CAED,MAAM,eAAe,oBAKlB;CAEH,MAAMC,UAA6B,OAAO,YAAY;EACpD,MAAM,OAAO;GACX,GAAG;GACH,GAAG;GACH,OAAO,QAAQ,SAAS,QAAQ,SAAS,WAAW;GACpD,SAAS,aAAa,QAAQ,SAAS,QAAQ,QAAQ;EACxD;AAGD,MAAI,CAAC,QAAQ,WAAW,QAAQ,MAAM,UAAU;GAC9C,MAAM,SAAS,UAAU,QAAQ,KAAK,UAAoB,KAAK,OAAO;AACtE,OAAI,QACF,KAAK,UAAU,qBAAqB,QAAQ,QAAQ;EAEvD,WAAU,CAAC,QAAQ,WAAW,KAAK,QAClC,KAAK,UAAU,qBAAqB,KAAK,QAAQ,QAAQ;AAG3D,MAAI,KAAK,UACP,MAAM,cAAc;GAClB,GAAG;GACH,UAAU,KAAK;EAChB,EAAC;AAGJ,MAAI,KAAK,kBACP,MAAM,KAAK,iBAAiB,KAAK;AAGnC,MAAI,KAAK,QAAQ,KAAK,gBACpB,KAAK,OAAO,KAAK,eAAe,KAAK,KAAK;AAI5C,MAAI,KAAK,SAAS,UAAa,KAAK,SAAS,IAC3C,KAAK,QAAQ,OAAO,eAAe;EAGrC,MAAM,MAAM,SAAS,KAAK;EAC1B,MAAMC,cAAuB;GAC3B,UAAU;GACV,GAAG;EACJ;EAED,IAAIC,YAAU,IAAI,QAAQ,KAAK;AAE/B,OAAK,MAAM,MAAM,aAAa,QAAQ,KACpC,KAAI,IACFA,YAAU,MAAM,GAAGA,WAAS,KA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G;GAAS,QAAQ;EAAO,EAAC;EACxD;EACA;EACA,OAAO,CAAC,YAAY,QAAQ;GAAE,GAAG;GAAS,QAAQ;EAAS,EAAC;CAC7D;AACF"}
|
|
1
|
+
{"version":3,"file":"client.mjs","names":["config: Config","config","request: Client['request']","requestInit: ReqInit","request","data: any","jsonError: unknown","fetchFn: any","url: string","retryConfig: { maxRetries: number; retryDelay: number }","attempt: number","delay"],"sources":["../../src/client/client.ts"],"sourcesContent":["import { getManagementBaseUrl, getRegion } from '@storyblok/region-helper';\nimport type { Client, Config, RequestOptions } from './types';\nimport { ClientError } from './error';\nimport {\n buildUrl,\n createConfig,\n createInterceptors,\n getParseAs,\n mergeConfigs,\n mergeHeaders,\n setAuthParams,\n} from './utils';\nimport { calculateRetryDelay } from \"../utils/calculate-retry-delay\";\nimport { delay } from \"../utils/delay\";\n\ntype ReqInit = Omit<RequestInit, 'body' | 'headers'> & {\n body?: any;\n headers: ReturnType<typeof mergeHeaders>;\n};\n\nexport const createClient = (config: Config): Client => {\n let _config = mergeConfigs(createConfig(), config);\n\n const getConfig = (): Config => ({ ..._config });\n\n const setConfig = (config: Config): Config => {\n _config = mergeConfigs(_config, config);\n return getConfig();\n };\n\n const interceptors = createInterceptors<\n Request,\n Response,\n unknown,\n RequestOptions\n >();\n\n const request: Client['request'] = async (options) => {\n const opts = {\n ..._config,\n ...options,\n fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,\n headers: mergeHeaders(_config.headers, options.headers),\n };\n\n // If the baseUrl is not set and we have a space_id, we can attempt to infer the region\n if (!_config.baseUrl && options.path?.space_id) {\n const region = getRegion(options.path.space_id as number, opts.region);\n if (region) {\n opts.baseUrl = getManagementBaseUrl(region, 'https');\n }\n } else if (!_config.baseUrl && opts.region) {\n opts.baseUrl = getManagementBaseUrl(opts.region, 'https');\n }\n\n if (opts.security) {\n await setAuthParams({\n ...opts,\n security: opts.security,\n });\n }\n\n if (opts.requestValidator) {\n await opts.requestValidator(opts);\n }\n\n if (opts.body && opts.bodySerializer) {\n opts.body = opts.bodySerializer(opts.body);\n }\n\n // remove Content-Type header if body is empty to avoid sending invalid requests\n if (opts.body === undefined || opts.body === '') {\n opts.headers.delete('Content-Type');\n }\n\n const url = buildUrl(opts);\n const requestInit: ReqInit = {\n redirect: 'follow',\n ...opts,\n };\n\n let request = new Request(url, requestInit);\n\n for (const fn of interceptors.request._fns) {\n if (fn) {\n request = await fn(request, opts);\n }\n }\n\n // fetch must be assigned here, otherwise it would throw the error:\n // TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation\n const _fetch = opts.fetch!;\n \n // Execute with retry logic by recreating the request for each attempt\n let response = await executeWithRetry(_fetch, url, requestInit, {\n maxRetries: 12,\n retryDelay: 1000\n });\n\n for (const fn of interceptors.response._fns) {\n if (fn) {\n response = await fn(response, request, opts);\n }\n }\n\n const result = {\n request,\n response,\n };\n\n if (response.ok) {\n if (\n response.status === 204 ||\n response.headers.get('Content-Length') === '0'\n ) {\n return opts.responseStyle === 'data'\n ? {}\n : {\n data: {},\n ...result,\n };\n }\n\n const parseAs =\n (opts.parseAs === 'auto'\n ? getParseAs(response.headers.get('Content-Type'))\n : opts.parseAs) ?? 'json';\n\n let data: any;\n switch (parseAs) {\n case 'arrayBuffer':\n case 'blob':\n case 'formData':\n case 'json':\n case 'text':\n data = await response[parseAs]();\n break;\n case 'stream':\n return opts.responseStyle === 'data'\n ? response.body\n : {\n data: response.body,\n ...result,\n };\n }\n\n if (parseAs === 'json') {\n if (opts.responseValidator) {\n await opts.responseValidator(data);\n }\n\n if (opts.responseTransformer) {\n data = await opts.responseTransformer(data);\n }\n }\n\n return opts.responseStyle === 'data'\n ? data\n : {\n data,\n ...result,\n };\n }\n\n const textError = await response.text();\n let jsonError: unknown;\n\n try {\n jsonError = JSON.parse(textError);\n } catch {\n // noop\n }\n\n const error = jsonError ?? textError;\n let finalError = error;\n\n for (const fn of interceptors.error._fns) {\n if (fn) {\n finalError = (await fn(error, response, request, opts)) as string;\n }\n }\n\n finalError = finalError || ({} as string);\n\n if (opts.throwOnError) {\n throw new ClientError(response.statusText || 'API request failed', {\n status: response.status,\n statusText: response.statusText,\n data: finalError,\n });\n }\n\n // TODO: we probably want to return error and improve types\n return opts.responseStyle === 'data'\n ? undefined\n : {\n error: finalError,\n ...result,\n };\n };\n\n // Helper function to execute fetch with retry logic\n async function executeWithRetry(\n fetchFn: any,\n url: string,\n requestInit: ReqInit,\n retryConfig: { maxRetries: number; retryDelay: number },\n attempt: number = 0\n ): Promise<Response> {\n try {\n const request = new Request(url, requestInit);\n const response = await fetchFn(request);\n \n if (response.status === 429 && attempt < retryConfig.maxRetries) {\n const retryAfter = response.headers.get('retry-after');\n const retryDelay = retryAfter ? parseInt(retryAfter) * 1000 : calculateRetryDelay(attempt, retryConfig.retryDelay);\n await delay(retryDelay);\n \n // Use the original unconsumed request for retry\n return executeWithRetry(fetchFn, url, requestInit, retryConfig, attempt + 1);\n }\n \n return response;\n } catch (error) {\n // If it's a network error and we haven't exceeded retries, try again\n if (attempt < retryConfig.maxRetries) {\n const delay = retryConfig.retryDelay;\n await new Promise(resolve => setTimeout(resolve, delay));\n \n // Use the original unconsumed request for retry\n return executeWithRetry(fetchFn, url, requestInit, retryConfig, attempt + 1);\n }\n \n throw error;\n }\n }\n\n return {\n buildUrl,\n connect: (options) => request({ ...options, method: 'CONNECT' }),\n delete: (options) => request({ ...options, method: 'DELETE' }),\n get: (options) => request({ ...options, method: 'GET' }),\n getConfig,\n head: (options) => request({ ...options, method: 'HEAD' }),\n interceptors,\n options: (options) => request({ ...options, method: 'OPTIONS' }),\n patch: (options) => request({ ...options, method: 'PATCH' }),\n post: (options) => request({ ...options, method: 'POST' }),\n put: (options) => request({ ...options, method: 'PUT' }),\n request,\n setConfig,\n trace: (options) => request({ ...options, method: 'TRACE' }),\n };\n};\n"],"mappings":";;;;;;;AAoBA,MAAa,eAAe,CAACA,WAA2B;CACtD,IAAI,UAAU,aAAa,cAAc,EAAE,OAAO;CAElD,MAAM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C;CAC7D;AACF"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/client/error.d.ts
|
|
2
|
+
declare class ClientError extends Error {
|
|
3
|
+
readonly status: number;
|
|
4
|
+
readonly statusText: string;
|
|
5
|
+
readonly data: unknown;
|
|
6
|
+
readonly response: {
|
|
7
|
+
status: number;
|
|
8
|
+
statusText: string;
|
|
9
|
+
data: unknown;
|
|
10
|
+
};
|
|
11
|
+
constructor(message: string, options: {
|
|
12
|
+
status: number;
|
|
13
|
+
statusText: string;
|
|
14
|
+
data: unknown;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
export { ClientError };
|
|
19
|
+
//# sourceMappingURL=error.d.mts.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/client/error.d.ts
|
|
2
|
+
declare class ClientError extends Error {
|
|
3
|
+
readonly status: number;
|
|
4
|
+
readonly statusText: string;
|
|
5
|
+
readonly data: unknown;
|
|
6
|
+
readonly response: {
|
|
7
|
+
status: number;
|
|
8
|
+
statusText: string;
|
|
9
|
+
data: unknown;
|
|
10
|
+
};
|
|
11
|
+
constructor(message: string, options: {
|
|
12
|
+
status: number;
|
|
13
|
+
statusText: string;
|
|
14
|
+
data: unknown;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
export { ClientError };
|
|
19
|
+
//# sourceMappingURL=error.d.ts.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/client/error.ts
|
|
3
|
+
var ClientError = class extends Error {
|
|
4
|
+
status;
|
|
5
|
+
statusText;
|
|
6
|
+
data;
|
|
7
|
+
response;
|
|
8
|
+
constructor(message, options) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "ClientError";
|
|
11
|
+
this.status = options.status;
|
|
12
|
+
this.statusText = options.statusText;
|
|
13
|
+
this.data = options.data;
|
|
14
|
+
this.response = {
|
|
15
|
+
status: options.status,
|
|
16
|
+
statusText: options.statusText,
|
|
17
|
+
data: options.data
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
exports.ClientError = ClientError;
|
|
24
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","names":["message: string","options: {\n status: number;\n statusText: string;\n data: unknown;\n }"],"sources":["../../src/client/error.ts"],"sourcesContent":["export class ClientError extends Error {\n readonly status: number;\n readonly statusText: string;\n readonly data: unknown;\n readonly response: { status: number; statusText: string; data: unknown };\n\n constructor(message: string, options: {\n status: number;\n statusText: string;\n data: unknown;\n }) {\n super(message);\n this.name = 'ClientError';\n this.status = options.status;\n this.statusText = options.statusText;\n this.data = options.data;\n this.response = { status: options.status, statusText: options.statusText, data: options.data };\n }\n}\n"],"mappings":";;AAAA,IAAa,cAAb,cAAiC,MAAM;CACrC,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAS;CAET,YAAYA,SAAiBC,SAI1B;EACD,MAAM,QAAQ;EACd,KAAK,OAAO;EACZ,KAAK,SAAS,QAAQ;EACtB,KAAK,aAAa,QAAQ;EAC1B,KAAK,OAAO,QAAQ;EACpB,KAAK,WAAW;GAAE,QAAQ,QAAQ;GAAQ,YAAY,QAAQ;GAAY,MAAM,QAAQ;EAAM;CAC/F;AACF"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/client/error.ts
|
|
2
|
+
var ClientError = class extends Error {
|
|
3
|
+
status;
|
|
4
|
+
statusText;
|
|
5
|
+
data;
|
|
6
|
+
response;
|
|
7
|
+
constructor(message, options) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "ClientError";
|
|
10
|
+
this.status = options.status;
|
|
11
|
+
this.statusText = options.statusText;
|
|
12
|
+
this.data = options.data;
|
|
13
|
+
this.response = {
|
|
14
|
+
status: options.status,
|
|
15
|
+
statusText: options.statusText,
|
|
16
|
+
data: options.data
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
export { ClientError };
|
|
23
|
+
//# sourceMappingURL=error.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.mjs","names":["message: string","options: {\n status: number;\n statusText: string;\n data: unknown;\n }"],"sources":["../../src/client/error.ts"],"sourcesContent":["export class ClientError extends Error {\n readonly status: number;\n readonly statusText: string;\n readonly data: unknown;\n readonly response: { status: number; statusText: string; data: unknown };\n\n constructor(message: string, options: {\n status: number;\n statusText: string;\n data: unknown;\n }) {\n super(message);\n this.name = 'ClientError';\n this.status = options.status;\n this.statusText = options.statusText;\n this.data = options.data;\n this.response = { status: options.status, statusText: options.statusText, data: options.data };\n }\n}\n"],"mappings":";AAAA,IAAa,cAAb,cAAiC,MAAM;CACrC,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAS;CAET,YAAYA,SAAiBC,SAI1B;EACD,MAAM,QAAQ;EACd,KAAK,OAAO;EACZ,KAAK,SAAS,QAAQ;EACtB,KAAK,aAAa,QAAQ;EAC1B,KAAK,OAAO,QAAQ;EACpB,KAAK,WAAW;GAAE,QAAQ,QAAQ;GAAQ,YAAY,QAAQ;GAAY,MAAM,QAAQ;EAAM;CAC/F;AACF"}
|
package/dist/client/index.js
CHANGED
package/dist/client/index.mjs
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -11,6 +11,7 @@ import { types_gen_d_exports as types_gen_d_exports$9 } from "./generated/storie
|
|
|
11
11
|
import { types_gen_d_exports as types_gen_d_exports$10 } from "./generated/users/types.gen.mjs";
|
|
12
12
|
import { Client } from "./client/types.mjs";
|
|
13
13
|
import { SdkRegistryInstance } from "./sdk-registry.generated.mjs";
|
|
14
|
+
import { ClientError } from "./client/error.mjs";
|
|
14
15
|
import { createClient } from "./client/client.mjs";
|
|
15
16
|
import { Region } from "@storyblok/region-helper";
|
|
16
17
|
|
|
@@ -57,5 +58,5 @@ declare class ManagementApiClient<ThrowOnError extends boolean = false> {
|
|
|
57
58
|
setToken(token: PersonalAccessToken | OAuthToken): void;
|
|
58
59
|
}
|
|
59
60
|
//#endregion
|
|
60
|
-
export { types_gen_d_exports as AssetFolders, types_gen_d_exports$1 as Assets, Client, types_gen_d_exports$2 as ComponentFolders, types_gen_d_exports$3 as Components, types_gen_d_exports$4 as DatasourceEntries, types_gen_d_exports$5 as Datasources, types_gen_d_exports$6 as InternalTags, ManagementApiClient, ManagementApiClientConfig, types_gen_d_exports$7 as Presets, types_gen_d_exports$8 as Spaces, types_gen_d_exports$9 as Stories, types_gen_d_exports$10 as Users, createClient };
|
|
61
|
+
export { types_gen_d_exports as AssetFolders, types_gen_d_exports$1 as Assets, Client, ClientError, types_gen_d_exports$2 as ComponentFolders, types_gen_d_exports$3 as Components, types_gen_d_exports$4 as DatasourceEntries, types_gen_d_exports$5 as Datasources, types_gen_d_exports$6 as InternalTags, ManagementApiClient, ManagementApiClientConfig, types_gen_d_exports$7 as Presets, types_gen_d_exports$8 as Spaces, types_gen_d_exports$9 as Stories, types_gen_d_exports$10 as Users, createClient };
|
|
61
62
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ import { types_gen_d_exports as types_gen_d_exports$9 } from "./generated/storie
|
|
|
11
11
|
import { types_gen_d_exports as types_gen_d_exports$10 } from "./generated/users/types.gen.js";
|
|
12
12
|
import { Client } from "./client/types.js";
|
|
13
13
|
import { SdkRegistryInstance } from "./sdk-registry.generated.js";
|
|
14
|
+
import { ClientError } from "./client/error.js";
|
|
14
15
|
import { createClient } from "./client/client.js";
|
|
15
16
|
import { Region } from "@storyblok/region-helper";
|
|
16
17
|
|
|
@@ -57,5 +58,5 @@ declare class ManagementApiClient<ThrowOnError extends boolean = false> {
|
|
|
57
58
|
setToken(token: PersonalAccessToken | OAuthToken): void;
|
|
58
59
|
}
|
|
59
60
|
//#endregion
|
|
60
|
-
export { types_gen_d_exports as AssetFolders, types_gen_d_exports$1 as Assets, Client, types_gen_d_exports$2 as ComponentFolders, types_gen_d_exports$3 as Components, types_gen_d_exports$4 as DatasourceEntries, types_gen_d_exports$5 as Datasources, types_gen_d_exports$6 as InternalTags, ManagementApiClient, ManagementApiClientConfig, types_gen_d_exports$7 as Presets, types_gen_d_exports$8 as Spaces, types_gen_d_exports$9 as Stories, types_gen_d_exports$10 as Users, createClient };
|
|
61
|
+
export { types_gen_d_exports as AssetFolders, types_gen_d_exports$1 as Assets, Client, ClientError, types_gen_d_exports$2 as ComponentFolders, types_gen_d_exports$3 as Components, types_gen_d_exports$4 as DatasourceEntries, types_gen_d_exports$5 as Datasources, types_gen_d_exports$6 as InternalTags, ManagementApiClient, ManagementApiClientConfig, types_gen_d_exports$7 as Presets, types_gen_d_exports$8 as Spaces, types_gen_d_exports$9 as Stories, types_gen_d_exports$10 as Users, createClient };
|
|
61
62
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const require_error = require('./client/error.js');
|
|
1
2
|
const require_client = require('./client/client.js');
|
|
2
3
|
require('./client/index.js');
|
|
3
4
|
const require_sdk_registry_generated = require('./sdk-registry.generated.js');
|
|
@@ -104,6 +105,7 @@ Object.defineProperty(exports, 'Assets', {
|
|
|
104
105
|
return require_generated_assets_types_gen.types_gen_exports;
|
|
105
106
|
}
|
|
106
107
|
});
|
|
108
|
+
exports.ClientError = require_error.ClientError;
|
|
107
109
|
Object.defineProperty(exports, 'ComponentFolders', {
|
|
108
110
|
enumerable: true,
|
|
109
111
|
get: function () {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["config: ManagementApiClientConfig<ThrowOnError>","sdkRegistry","config: Partial<Omit<ManagementApiClientConfig, 'token'>>","token: PersonalAccessToken | OAuthToken","createClient"],"sources":["../src/index.ts"],"sourcesContent":["// Import generated SDKs with shared client support\nimport { createClient } from './client';\nimport type { Client } from './client/types';\nimport { getManagementBaseUrl, type Region } from '@storyblok/region-helper';\nimport { sdkRegistry, SdkRegistryInstance } from './sdk-registry.generated';\n\ntype PersonalAccessToken = {\n accessToken: string;\n}\n\ntype OAuthToken = {\n oauthToken: string;\n}\n\nexport interface ManagementApiClientConfig<ThrowOnError extends boolean = false> {\n token: PersonalAccessToken | OAuthToken;\n region?: Region;\n baseUrl?: string; // Override for custom endpoints\n headers?: Record<string, string>;\n throwOnError?: ThrowOnError;\n}\n\nexport interface ManagementApiClient<ThrowOnError extends boolean = false> extends SdkRegistryInstance {}\nexport class ManagementApiClient<ThrowOnError extends boolean = false> {\n protected client: Client;\n protected config: ManagementApiClientConfig<ThrowOnError>;\n protected sdkCache: Record<string, Promise<any>> = {};\n\n constructor(config: ManagementApiClientConfig<ThrowOnError>) {\n this.config = config;\n this.client = createClientInstance(config);\n\n Object.entries(sdkRegistry).forEach(([name, Sdk]) => {\n Object.defineProperty(this, name, {\n get: () => new Sdk({ client: this.client as any }),\n enumerable: true,\n configurable: true\n });\n });\n }\n\n\n /**\n * @returns The client's interceptors\n * @example\n * client.interceptors.request.use((request, options) => {\n * console.log('Request:', request);\n * return request;\n * });\n */\n public get interceptors(): Client['interceptors'] {\n return this.client.interceptors;\n }\n\n /**\n * @param config - The configuration to set\n * @example\n * client.setConfig({ region: 'eu' });\n */\n setConfig(config: Partial<Omit<ManagementApiClientConfig, 'token'>>): void {\n const { region, baseUrl, headers } = config;\n \n if (headers) {\n this.client.setConfig({\n baseUrl: baseUrl || this.config.baseUrl,\n region: region || this.config.region!,\n headers: {\n 'Content-Type': 'application/json',\n ...getAuthHeader(this.config.token),\n ...headers\n }\n });\n }\n }\n\n /**\n * @param token - The token to set\n * @example\n * client.setToken({ accessToken: '123' });\n */\n setToken(token: PersonalAccessToken | OAuthToken): void {\n this.config.token = token;\n this.client.setConfig({\n region: this.config.region ?? \"eu\",\n headers: {\n 'Content-Type': 'application/json',\n ...getAuthHeader(token),\n ...this.config.headers\n }\n });\n }\n}\n\n// Pure functions for client creation and setup\nfunction createClientInstance<ThrowOnError extends boolean = false>(\n config: ManagementApiClientConfig<ThrowOnError>\n): Client {\n const { token, region = \"eu\", baseUrl, headers = {}, throwOnError = false } = config;\n \n return createClient({\n baseUrl,\n region,\n headers: {\n 'Content-Type': 'application/json',\n ...getAuthHeader(token),\n ...headers\n },\n throwOnError: throwOnError ?? false\n });\n}\n\nfunction getAuthHeader(token: PersonalAccessToken | OAuthToken): Record<string, string> {\n return 'accessToken' in token ? {\n 'Authorization': token.accessToken\n } : {\n 'Authorization': token.oauthToken\n }\n}\n\n// Export client utilities\nexport { createClient } from './client';\nexport type { Client } from './client/types';\n\n// Export all generated types\nexport * from './types.generated';\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","names":["config: ManagementApiClientConfig<ThrowOnError>","sdkRegistry","config: Partial<Omit<ManagementApiClientConfig, 'token'>>","token: PersonalAccessToken | OAuthToken","createClient"],"sources":["../src/index.ts"],"sourcesContent":["// Import generated SDKs with shared client support\nimport { createClient } from './client';\nimport type { Client } from './client/types';\nimport { getManagementBaseUrl, type Region } from '@storyblok/region-helper';\nimport { sdkRegistry, SdkRegistryInstance } from './sdk-registry.generated';\n\ntype PersonalAccessToken = {\n accessToken: string;\n}\n\ntype OAuthToken = {\n oauthToken: string;\n}\n\nexport interface ManagementApiClientConfig<ThrowOnError extends boolean = false> {\n token: PersonalAccessToken | OAuthToken;\n region?: Region;\n baseUrl?: string; // Override for custom endpoints\n headers?: Record<string, string>;\n throwOnError?: ThrowOnError;\n}\n\nexport interface ManagementApiClient<ThrowOnError extends boolean = false> extends SdkRegistryInstance {}\nexport class ManagementApiClient<ThrowOnError extends boolean = false> {\n protected client: Client;\n protected config: ManagementApiClientConfig<ThrowOnError>;\n protected sdkCache: Record<string, Promise<any>> = {};\n\n constructor(config: ManagementApiClientConfig<ThrowOnError>) {\n this.config = config;\n this.client = createClientInstance(config);\n\n Object.entries(sdkRegistry).forEach(([name, Sdk]) => {\n Object.defineProperty(this, name, {\n get: () => new Sdk({ client: this.client as any }),\n enumerable: true,\n configurable: true\n });\n });\n }\n\n\n /**\n * @returns The client's interceptors\n * @example\n * client.interceptors.request.use((request, options) => {\n * console.log('Request:', request);\n * return request;\n * });\n */\n public get interceptors(): Client['interceptors'] {\n return this.client.interceptors;\n }\n\n /**\n * @param config - The configuration to set\n * @example\n * client.setConfig({ region: 'eu' });\n */\n setConfig(config: Partial<Omit<ManagementApiClientConfig, 'token'>>): void {\n const { region, baseUrl, headers } = config;\n \n if (headers) {\n this.client.setConfig({\n baseUrl: baseUrl || this.config.baseUrl,\n region: region || this.config.region!,\n headers: {\n 'Content-Type': 'application/json',\n ...getAuthHeader(this.config.token),\n ...headers\n }\n });\n }\n }\n\n /**\n * @param token - The token to set\n * @example\n * client.setToken({ accessToken: '123' });\n */\n setToken(token: PersonalAccessToken | OAuthToken): void {\n this.config.token = token;\n this.client.setConfig({\n region: this.config.region ?? \"eu\",\n headers: {\n 'Content-Type': 'application/json',\n ...getAuthHeader(token),\n ...this.config.headers\n }\n });\n }\n}\n\n// Pure functions for client creation and setup\nfunction createClientInstance<ThrowOnError extends boolean = false>(\n config: ManagementApiClientConfig<ThrowOnError>\n): Client {\n const { token, region = \"eu\", baseUrl, headers = {}, throwOnError = false } = config;\n \n return createClient({\n baseUrl,\n region,\n headers: {\n 'Content-Type': 'application/json',\n ...getAuthHeader(token),\n ...headers\n },\n throwOnError: throwOnError ?? false\n });\n}\n\nfunction getAuthHeader(token: PersonalAccessToken | OAuthToken): Record<string, string> {\n return 'accessToken' in token ? {\n 'Authorization': token.accessToken\n } : {\n 'Authorization': token.oauthToken\n }\n}\n\n// Export client utilities\nexport { ClientError } from './client/error';\nexport { createClient } from './client';\nexport type { Client } from './client/types';\n\n// Export all generated types\nexport * from './types.generated';\n"],"mappings":";;;;;;;;;;;;;;;;;AAuBA,IAAa,sBAAb,MAAuE;CACrE,AA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-1,3 +1,4 @@
|
|
|
1
|
+
import { ClientError } from "./client/error.mjs";
|
|
1
2
|
import { createClient } from "./client/client.mjs";
|
|
2
3
|
import "./client/index.mjs";
|
|
3
4
|
import { sdkRegistry } from "./sdk-registry.generated.mjs";
|
|
@@ -92,5 +93,5 @@ function getAuthHeader(token) {
|
|
|
92
93
|
}
|
|
93
94
|
|
|
94
95
|
//#endregion
|
|
95
|
-
export { types_gen_exports$10 as AssetFolders, types_gen_exports$9 as Assets, types_gen_exports$8 as ComponentFolders, types_gen_exports$7 as Components, types_gen_exports$6 as DatasourceEntries, types_gen_exports$5 as Datasources, types_gen_exports$4 as InternalTags, ManagementApiClient, types_gen_exports$3 as Presets, types_gen_exports$2 as Spaces, types_gen_exports$1 as Stories, types_gen_exports as Users, createClient };
|
|
96
|
+
export { types_gen_exports$10 as AssetFolders, types_gen_exports$9 as Assets, ClientError, types_gen_exports$8 as ComponentFolders, types_gen_exports$7 as Components, types_gen_exports$6 as DatasourceEntries, types_gen_exports$5 as Datasources, types_gen_exports$4 as InternalTags, ManagementApiClient, types_gen_exports$3 as Presets, types_gen_exports$2 as Spaces, types_gen_exports$1 as Stories, types_gen_exports as Users, createClient };
|
|
96
97
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["config: ManagementApiClientConfig<ThrowOnError>","config: Partial<Omit<ManagementApiClientConfig, 'token'>>","token: PersonalAccessToken | OAuthToken"],"sources":["../src/index.ts"],"sourcesContent":["// Import generated SDKs with shared client support\nimport { createClient } from './client';\nimport type { Client } from './client/types';\nimport { getManagementBaseUrl, type Region } from '@storyblok/region-helper';\nimport { sdkRegistry, SdkRegistryInstance } from './sdk-registry.generated';\n\ntype PersonalAccessToken = {\n accessToken: string;\n}\n\ntype OAuthToken = {\n oauthToken: string;\n}\n\nexport interface ManagementApiClientConfig<ThrowOnError extends boolean = false> {\n token: PersonalAccessToken | OAuthToken;\n region?: Region;\n baseUrl?: string; // Override for custom endpoints\n headers?: Record<string, string>;\n throwOnError?: ThrowOnError;\n}\n\nexport interface ManagementApiClient<ThrowOnError extends boolean = false> extends SdkRegistryInstance {}\nexport class ManagementApiClient<ThrowOnError extends boolean = false> {\n protected client: Client;\n protected config: ManagementApiClientConfig<ThrowOnError>;\n protected sdkCache: Record<string, Promise<any>> = {};\n\n constructor(config: ManagementApiClientConfig<ThrowOnError>) {\n this.config = config;\n this.client = createClientInstance(config);\n\n Object.entries(sdkRegistry).forEach(([name, Sdk]) => {\n Object.defineProperty(this, name, {\n get: () => new Sdk({ client: this.client as any }),\n enumerable: true,\n configurable: true\n });\n });\n }\n\n\n /**\n * @returns The client's interceptors\n * @example\n * client.interceptors.request.use((request, options) => {\n * console.log('Request:', request);\n * return request;\n * });\n */\n public get interceptors(): Client['interceptors'] {\n return this.client.interceptors;\n }\n\n /**\n * @param config - The configuration to set\n * @example\n * client.setConfig({ region: 'eu' });\n */\n setConfig(config: Partial<Omit<ManagementApiClientConfig, 'token'>>): void {\n const { region, baseUrl, headers } = config;\n \n if (headers) {\n this.client.setConfig({\n baseUrl: baseUrl || this.config.baseUrl,\n region: region || this.config.region!,\n headers: {\n 'Content-Type': 'application/json',\n ...getAuthHeader(this.config.token),\n ...headers\n }\n });\n }\n }\n\n /**\n * @param token - The token to set\n * @example\n * client.setToken({ accessToken: '123' });\n */\n setToken(token: PersonalAccessToken | OAuthToken): void {\n this.config.token = token;\n this.client.setConfig({\n region: this.config.region ?? \"eu\",\n headers: {\n 'Content-Type': 'application/json',\n ...getAuthHeader(token),\n ...this.config.headers\n }\n });\n }\n}\n\n// Pure functions for client creation and setup\nfunction createClientInstance<ThrowOnError extends boolean = false>(\n config: ManagementApiClientConfig<ThrowOnError>\n): Client {\n const { token, region = \"eu\", baseUrl, headers = {}, throwOnError = false } = config;\n \n return createClient({\n baseUrl,\n region,\n headers: {\n 'Content-Type': 'application/json',\n ...getAuthHeader(token),\n ...headers\n },\n throwOnError: throwOnError ?? false\n });\n}\n\nfunction getAuthHeader(token: PersonalAccessToken | OAuthToken): Record<string, string> {\n return 'accessToken' in token ? {\n 'Authorization': token.accessToken\n } : {\n 'Authorization': token.oauthToken\n }\n}\n\n// Export client utilities\nexport { createClient } from './client';\nexport type { Client } from './client/types';\n\n// Export all generated types\nexport * from './types.generated';\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["config: ManagementApiClientConfig<ThrowOnError>","config: Partial<Omit<ManagementApiClientConfig, 'token'>>","token: PersonalAccessToken | OAuthToken"],"sources":["../src/index.ts"],"sourcesContent":["// Import generated SDKs with shared client support\nimport { createClient } from './client';\nimport type { Client } from './client/types';\nimport { getManagementBaseUrl, type Region } from '@storyblok/region-helper';\nimport { sdkRegistry, SdkRegistryInstance } from './sdk-registry.generated';\n\ntype PersonalAccessToken = {\n accessToken: string;\n}\n\ntype OAuthToken = {\n oauthToken: string;\n}\n\nexport interface ManagementApiClientConfig<ThrowOnError extends boolean = false> {\n token: PersonalAccessToken | OAuthToken;\n region?: Region;\n baseUrl?: string; // Override for custom endpoints\n headers?: Record<string, string>;\n throwOnError?: ThrowOnError;\n}\n\nexport interface ManagementApiClient<ThrowOnError extends boolean = false> extends SdkRegistryInstance {}\nexport class ManagementApiClient<ThrowOnError extends boolean = false> {\n protected client: Client;\n protected config: ManagementApiClientConfig<ThrowOnError>;\n protected sdkCache: Record<string, Promise<any>> = {};\n\n constructor(config: ManagementApiClientConfig<ThrowOnError>) {\n this.config = config;\n this.client = createClientInstance(config);\n\n Object.entries(sdkRegistry).forEach(([name, Sdk]) => {\n Object.defineProperty(this, name, {\n get: () => new Sdk({ client: this.client as any }),\n enumerable: true,\n configurable: true\n });\n });\n }\n\n\n /**\n * @returns The client's interceptors\n * @example\n * client.interceptors.request.use((request, options) => {\n * console.log('Request:', request);\n * return request;\n * });\n */\n public get interceptors(): Client['interceptors'] {\n return this.client.interceptors;\n }\n\n /**\n * @param config - The configuration to set\n * @example\n * client.setConfig({ region: 'eu' });\n */\n setConfig(config: Partial<Omit<ManagementApiClientConfig, 'token'>>): void {\n const { region, baseUrl, headers } = config;\n \n if (headers) {\n this.client.setConfig({\n baseUrl: baseUrl || this.config.baseUrl,\n region: region || this.config.region!,\n headers: {\n 'Content-Type': 'application/json',\n ...getAuthHeader(this.config.token),\n ...headers\n }\n });\n }\n }\n\n /**\n * @param token - The token to set\n * @example\n * client.setToken({ accessToken: '123' });\n */\n setToken(token: PersonalAccessToken | OAuthToken): void {\n this.config.token = token;\n this.client.setConfig({\n region: this.config.region ?? \"eu\",\n headers: {\n 'Content-Type': 'application/json',\n ...getAuthHeader(token),\n ...this.config.headers\n }\n });\n }\n}\n\n// Pure functions for client creation and setup\nfunction createClientInstance<ThrowOnError extends boolean = false>(\n config: ManagementApiClientConfig<ThrowOnError>\n): Client {\n const { token, region = \"eu\", baseUrl, headers = {}, throwOnError = false } = config;\n \n return createClient({\n baseUrl,\n region,\n headers: {\n 'Content-Type': 'application/json',\n ...getAuthHeader(token),\n ...headers\n },\n throwOnError: throwOnError ?? false\n });\n}\n\nfunction getAuthHeader(token: PersonalAccessToken | OAuthToken): Record<string, string> {\n return 'accessToken' in token ? {\n 'Authorization': token.accessToken\n } : {\n 'Authorization': token.oauthToken\n }\n}\n\n// Export client utilities\nexport { ClientError } from './client/error';\nexport { createClient } from './client';\nexport type { Client } from './client/types';\n\n// Export all generated types\nexport * from './types.generated';\n"],"mappings":";;;;;;;;;;;;;;;;;AAuBA,IAAa,sBAAb,MAAuE;CACrE,AAAU;CACV,AAAU;CACV,AAAU,WAAyC,CAAE;CAErD,YAAYA,QAAiD;EAC3D,KAAK,SAAS;EACd,KAAK,SAAS,qBAAqB,OAAO;EAE1C,OAAO,QAAQ,YAAY,CAAC,QAAQ,CAAC,CAAC,MAAM,IAAI,KAAK;GACnD,OAAO,eAAe,MAAM,MAAM;IAChC,KAAK,MAAM,IAAI,IAAI,EAAE,QAAQ,KAAK,OAAe;IACjD,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