bitsnap-payloadcms 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +270 -2
- package/dist/client.d.ts +3 -0
- package/dist/client.js +5 -0
- package/dist/client.js.map +1 -0
- package/dist/constants.d.ts +7 -0
- package/dist/constants.js +8 -0
- package/dist/constants.js.map +1 -0
- package/dist/fields/getFields.d.ts +54 -0
- package/dist/fields/getFields.js +150 -0
- package/dist/fields/getFields.js.map +1 -0
- package/dist/fields/index.d.ts +2 -0
- package/dist/fields/index.js +3 -0
- package/dist/fields/index.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/plugin.d.ts +15 -0
- package/dist/plugin.js +60 -0
- package/dist/plugin.js.map +1 -0
- package/dist/server/client.d.ts +47 -0
- package/dist/server/client.js +68 -0
- package/dist/server/client.js.map +1 -0
- package/dist/server/endpoints.d.ts +9 -0
- package/dist/server/endpoints.js +190 -0
- package/dist/server/endpoints.js.map +1 -0
- package/dist/server/errors.d.ts +9 -0
- package/dist/server/errors.js +35 -0
- package/dist/server/errors.js.map +1 -0
- package/dist/server/index.d.ts +6 -0
- package/dist/server/index.js +6 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/schemas.d.ts +55 -0
- package/dist/server/schemas.js +46 -0
- package/dist/server/schemas.js.map +1 -0
- package/dist/types.d.ts +75 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/ui/BitsnapManager.css +159 -0
- package/dist/ui/BitsnapManager.d.ts +5 -0
- package/dist/ui/BitsnapManager.js +642 -0
- package/dist/ui/BitsnapManager.js.map +1 -0
- package/dist/ui/LinkToDoc.d.ts +4 -0
- package/dist/ui/LinkToDoc.js +48 -0
- package/dist/ui/LinkToDoc.js.map +1 -0
- package/dist/ui/ProductSelect.css +199 -0
- package/dist/ui/ProductSelect.d.ts +5 -0
- package/dist/ui/ProductSelect.js +146 -0
- package/dist/ui/ProductSelect.js.map +1 -0
- package/dist/ui/getBitsnapClientConfig.d.ts +13 -0
- package/dist/ui/getBitsnapClientConfig.js +30 -0
- package/dist/ui/getBitsnapClientConfig.js.map +1 -0
- package/dist/utils/formatCurrency.d.ts +6 -0
- package/dist/utils/formatCurrency.js +24 -0
- package/dist/utils/formatCurrency.js.map +1 -0
- package/package.json +93 -4
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { DEFAULT_API_BASE_PATH, DEFAULT_CURRENCY, DEFAULT_DASHBOARD_URL } from './constants.js';
|
|
2
|
+
import { DEFAULT_API_ROUTE, getFields } from './fields/getFields.js';
|
|
3
|
+
import { configureBitsnapBackend } from './server/client.js';
|
|
4
|
+
import { createBitsnapEndpoints } from './server/endpoints.js';
|
|
5
|
+
import { setBitsnapCurrency } from './server/schemas.js';
|
|
6
|
+
/**
|
|
7
|
+
* Fills in every default the plugin needs so the rest of the code can rely on
|
|
8
|
+
* a fully populated configuration.
|
|
9
|
+
*/ export function sanitizeBitsnapPluginConfig(pluginConfig) {
|
|
10
|
+
return {
|
|
11
|
+
...pluginConfig,
|
|
12
|
+
apiBasePath: pluginConfig.apiBasePath ?? DEFAULT_API_BASE_PATH,
|
|
13
|
+
currency: pluginConfig.currency ?? DEFAULT_CURRENCY,
|
|
14
|
+
dashboardUrl: pluginConfig.dashboardUrl ?? DEFAULT_DASHBOARD_URL,
|
|
15
|
+
projectId: pluginConfig.projectId ?? process.env.BITSNAP_PROJECT_ID ?? '',
|
|
16
|
+
sync: pluginConfig.sync ?? []
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Payload plugin that wires Bitsnap into a Payload project.
|
|
21
|
+
*
|
|
22
|
+
* - appends the Bitsnap fields to every collection listed in `sync`
|
|
23
|
+
* - mounts the Bitsnap REST endpoints on the Payload API root
|
|
24
|
+
* - points the Bitsnap SDK at the configured host and API key
|
|
25
|
+
*/ export const bitsnapPlugin = (pluginConfig)=>{
|
|
26
|
+
return (config)=>{
|
|
27
|
+
const sanitized = sanitizeBitsnapPluginConfig(pluginConfig);
|
|
28
|
+
setBitsnapCurrency(sanitized.currency);
|
|
29
|
+
configureBitsnapBackend({
|
|
30
|
+
apiKey: sanitized.bitsnapApiKey
|
|
31
|
+
});
|
|
32
|
+
const apiRoute = config.routes?.api ?? DEFAULT_API_ROUTE;
|
|
33
|
+
const collections = (config.collections ?? []).map((collection)=>{
|
|
34
|
+
const syncConfig = sanitized.sync.find((sync)=>sync.collection === collection.slug);
|
|
35
|
+
if (!syncConfig) {
|
|
36
|
+
return collection;
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
...collection,
|
|
40
|
+
fields: getFields({
|
|
41
|
+
apiRoute,
|
|
42
|
+
collection,
|
|
43
|
+
pluginConfig: sanitized,
|
|
44
|
+
syncConfig
|
|
45
|
+
})
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
const endpoints = [
|
|
49
|
+
...config.endpoints ?? [],
|
|
50
|
+
...createBitsnapEndpoints(sanitized)
|
|
51
|
+
];
|
|
52
|
+
return {
|
|
53
|
+
...config,
|
|
54
|
+
collections,
|
|
55
|
+
endpoints
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/plugin.ts"],"sourcesContent":["import type { Config, Plugin } from 'payload'\n\nimport {\n DEFAULT_API_BASE_PATH,\n DEFAULT_CURRENCY,\n DEFAULT_DASHBOARD_URL,\n} from './constants.js'\nimport { DEFAULT_API_ROUTE, getFields } from './fields/getFields.js'\nimport { configureBitsnapBackend } from './server/client.js'\nimport { createBitsnapEndpoints } from './server/endpoints.js'\nimport { setBitsnapCurrency } from './server/schemas.js'\nimport type { BitsnapPluginConfig, SanitizedBitsnapPluginConfig } from './types.js'\n\n/**\n * Fills in every default the plugin needs so the rest of the code can rely on\n * a fully populated configuration.\n */\nexport function sanitizeBitsnapPluginConfig(\n pluginConfig: BitsnapPluginConfig,\n): SanitizedBitsnapPluginConfig {\n return {\n ...pluginConfig,\n apiBasePath: pluginConfig.apiBasePath ?? DEFAULT_API_BASE_PATH,\n currency: pluginConfig.currency ?? DEFAULT_CURRENCY,\n dashboardUrl: pluginConfig.dashboardUrl ?? DEFAULT_DASHBOARD_URL,\n projectId: pluginConfig.projectId ?? process.env.BITSNAP_PROJECT_ID ?? '',\n sync: pluginConfig.sync ?? [],\n }\n}\n\n/**\n * Payload plugin that wires Bitsnap into a Payload project.\n *\n * - appends the Bitsnap fields to every collection listed in `sync`\n * - mounts the Bitsnap REST endpoints on the Payload API root\n * - points the Bitsnap SDK at the configured host and API key\n */\nexport const bitsnapPlugin = (pluginConfig: BitsnapPluginConfig): Plugin => {\n return (config: Config): Config => {\n const sanitized = sanitizeBitsnapPluginConfig(pluginConfig)\n\n setBitsnapCurrency(sanitized.currency)\n configureBitsnapBackend({ apiKey: sanitized.bitsnapApiKey })\n\n const apiRoute = config.routes?.api ?? DEFAULT_API_ROUTE\n\n const collections = (config.collections ?? []).map((collection) => {\n const syncConfig = sanitized.sync.find((sync) => sync.collection === collection.slug)\n\n if (!syncConfig) {\n return collection\n }\n\n return {\n ...collection,\n fields: getFields({\n apiRoute,\n collection,\n pluginConfig: sanitized,\n syncConfig,\n }),\n }\n })\n\n const endpoints = [...(config.endpoints ?? []), ...createBitsnapEndpoints(sanitized)]\n\n return {\n ...config,\n collections,\n endpoints,\n }\n }\n}\n"],"names":["DEFAULT_API_BASE_PATH","DEFAULT_CURRENCY","DEFAULT_DASHBOARD_URL","DEFAULT_API_ROUTE","getFields","configureBitsnapBackend","createBitsnapEndpoints","setBitsnapCurrency","sanitizeBitsnapPluginConfig","pluginConfig","apiBasePath","currency","dashboardUrl","projectId","process","env","BITSNAP_PROJECT_ID","sync","bitsnapPlugin","config","sanitized","apiKey","bitsnapApiKey","apiRoute","routes","api","collections","map","collection","syncConfig","find","slug","fields","endpoints"],"mappings":"AAEA,SACEA,qBAAqB,EACrBC,gBAAgB,EAChBC,qBAAqB,QAChB,iBAAgB;AACvB,SAASC,iBAAiB,EAAEC,SAAS,QAAQ,wBAAuB;AACpE,SAASC,uBAAuB,QAAQ,qBAAoB;AAC5D,SAASC,sBAAsB,QAAQ,wBAAuB;AAC9D,SAASC,kBAAkB,QAAQ,sBAAqB;AAGxD;;;CAGC,GACD,OAAO,SAASC,4BACdC,YAAiC;IAEjC,OAAO;QACL,GAAGA,YAAY;QACfC,aAAaD,aAAaC,WAAW,IAAIV;QACzCW,UAAUF,aAAaE,QAAQ,IAAIV;QACnCW,cAAcH,aAAaG,YAAY,IAAIV;QAC3CW,WAAWJ,aAAaI,SAAS,IAAIC,QAAQC,GAAG,CAACC,kBAAkB,IAAI;QACvEC,MAAMR,aAAaQ,IAAI,IAAI,EAAE;IAC/B;AACF;AAEA;;;;;;CAMC,GACD,OAAO,MAAMC,gBAAgB,CAACT;IAC5B,OAAO,CAACU;QACN,MAAMC,YAAYZ,4BAA4BC;QAE9CF,mBAAmBa,UAAUT,QAAQ;QACrCN,wBAAwB;YAAEgB,QAAQD,UAAUE,aAAa;QAAC;QAE1D,MAAMC,WAAWJ,OAAOK,MAAM,EAAEC,OAAOtB;QAEvC,MAAMuB,cAAc,AAACP,CAAAA,OAAOO,WAAW,IAAI,EAAE,AAAD,EAAGC,GAAG,CAAC,CAACC;YAClD,MAAMC,aAAaT,UAAUH,IAAI,CAACa,IAAI,CAAC,CAACb,OAASA,KAAKW,UAAU,KAAKA,WAAWG,IAAI;YAEpF,IAAI,CAACF,YAAY;gBACf,OAAOD;YACT;YAEA,OAAO;gBACL,GAAGA,UAAU;gBACbI,QAAQ5B,UAAU;oBAChBmB;oBACAK;oBACAnB,cAAcW;oBACdS;gBACF;YACF;QACF;QAEA,MAAMI,YAAY;eAAKd,OAAOc,SAAS,IAAI,EAAE;eAAM3B,uBAAuBc;SAAW;QAErF,OAAO;YACL,GAAGD,MAAM;YACTO;YACAO;QACF;IACF;AACF,EAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as BitsnapBackend from 'bitsnap-js/backend';
|
|
2
|
+
import type { CreateProductInput, ManagedProduct, ProductWriteResult, UpdateProductInput } from 'bitsnap-js/backend';
|
|
3
|
+
export type ConfigureBitsnapBackendArgs = {
|
|
4
|
+
/**
|
|
5
|
+
* Bitsnap API key. Falls back to the key the plugin was configured with and
|
|
6
|
+
* finally to `process.env.BITSNAP_API_KEY`.
|
|
7
|
+
*/
|
|
8
|
+
apiKey?: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Raw envelope returned by the Bitsnap product list endpoint.
|
|
12
|
+
*/
|
|
13
|
+
export type ShopProductsResult = Awaited<ReturnType<typeof BitsnapBackend.getProducts>>;
|
|
14
|
+
/**
|
|
15
|
+
* Resolves the API key that should be used for the Bitsnap calls.
|
|
16
|
+
*
|
|
17
|
+
* Order: explicit argument → key the plugin was configured with →
|
|
18
|
+
* `process.env.BITSNAP_API_KEY`.
|
|
19
|
+
*/
|
|
20
|
+
export declare function resolveBitsnapApiKey(apiKey?: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* True when a Bitsnap API key is available. Drafts can only be listed with a key.
|
|
23
|
+
*/
|
|
24
|
+
export declare function isBitsnapApiKeyConfigured(apiKey?: string): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Points the Bitsnap SDK at the right host and gives it the API key.
|
|
27
|
+
*/
|
|
28
|
+
export declare function configureBitsnapBackend({ apiKey }?: ConfigureBitsnapBackendArgs): void;
|
|
29
|
+
/**
|
|
30
|
+
* Reads a single managed product (including its `revision`) from Bitsnap.
|
|
31
|
+
*/
|
|
32
|
+
export declare function getManagedProduct(projectId: string, productId: string): Promise<ManagedProduct>;
|
|
33
|
+
/**
|
|
34
|
+
* Creates a managed product in Bitsnap.
|
|
35
|
+
*/
|
|
36
|
+
export declare function createManagedProduct(projectId: string, input: CreateProductInput): Promise<ProductWriteResult>;
|
|
37
|
+
/**
|
|
38
|
+
* Patches a managed product in Bitsnap.
|
|
39
|
+
*/
|
|
40
|
+
export declare function updateManagedProduct(projectId: string, productId: string, patch: UpdateProductInput): Promise<ProductWriteResult>;
|
|
41
|
+
/**
|
|
42
|
+
* Lists the products of a Bitsnap project.
|
|
43
|
+
*
|
|
44
|
+
* Drafts are only included when an API key is configured — the public endpoint
|
|
45
|
+
* does not serve them.
|
|
46
|
+
*/
|
|
47
|
+
export declare function getShopProducts(projectId: string, limit?: number): Promise<ShopProductsResult>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import * as BitsnapBackend from 'bitsnap-js/backend';
|
|
2
|
+
import { DEFAULT_PRODUCTS_LIMIT } from '../constants.js';
|
|
3
|
+
let configuredApiKey = null;
|
|
4
|
+
/**
|
|
5
|
+
* Resolves the API key that should be used for the Bitsnap calls.
|
|
6
|
+
*
|
|
7
|
+
* Order: explicit argument → key the plugin was configured with →
|
|
8
|
+
* `process.env.BITSNAP_API_KEY`.
|
|
9
|
+
*/ export function resolveBitsnapApiKey(apiKey) {
|
|
10
|
+
if (typeof apiKey === 'string' && apiKey.length > 0) {
|
|
11
|
+
return apiKey;
|
|
12
|
+
}
|
|
13
|
+
if (typeof configuredApiKey === 'string' && configuredApiKey.length > 0) {
|
|
14
|
+
return configuredApiKey;
|
|
15
|
+
}
|
|
16
|
+
return process.env.BITSNAP_API_KEY ?? '';
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* True when a Bitsnap API key is available. Drafts can only be listed with a key.
|
|
20
|
+
*/ export function isBitsnapApiKeyConfigured(apiKey) {
|
|
21
|
+
return resolveBitsnapApiKey(apiKey).length > 0;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Points the Bitsnap SDK at the right host and gives it the API key.
|
|
25
|
+
*/ export function configureBitsnapBackend({ apiKey } = {}) {
|
|
26
|
+
const resolvedApiKey = resolveBitsnapApiKey(apiKey);
|
|
27
|
+
configuredApiKey = resolvedApiKey;
|
|
28
|
+
const customHost = process.env.CUSTOM_BITSNAP_HOST;
|
|
29
|
+
if (customHost != null && customHost.length > 0) {
|
|
30
|
+
void BitsnapBackend.setCustomHost(customHost);
|
|
31
|
+
}
|
|
32
|
+
void BitsnapBackend.setApiKey(resolvedApiKey);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Reads a single managed product (including its `revision`) from Bitsnap.
|
|
36
|
+
*/ export async function getManagedProduct(projectId, productId) {
|
|
37
|
+
configureBitsnapBackend();
|
|
38
|
+
return BitsnapBackend.getManagedProduct(projectId, productId);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Creates a managed product in Bitsnap.
|
|
42
|
+
*/ export async function createManagedProduct(projectId, input) {
|
|
43
|
+
configureBitsnapBackend();
|
|
44
|
+
return BitsnapBackend.createProduct(projectId, input);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Patches a managed product in Bitsnap.
|
|
48
|
+
*/ export async function updateManagedProduct(projectId, productId, patch) {
|
|
49
|
+
configureBitsnapBackend();
|
|
50
|
+
return BitsnapBackend.updateProduct(projectId, productId, patch);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Lists the products of a Bitsnap project.
|
|
54
|
+
*
|
|
55
|
+
* Drafts are only included when an API key is configured — the public endpoint
|
|
56
|
+
* does not serve them.
|
|
57
|
+
*/ export async function getShopProducts(projectId, limit = DEFAULT_PRODUCTS_LIMIT) {
|
|
58
|
+
configureBitsnapBackend();
|
|
59
|
+
const includeDrafts = isBitsnapApiKeyConfigured();
|
|
60
|
+
return BitsnapBackend.getProducts(projectId, limit, 0, {
|
|
61
|
+
cache: 'no-store'
|
|
62
|
+
}, {
|
|
63
|
+
groupVariants: true,
|
|
64
|
+
includeDrafts
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/server/client.ts"],"sourcesContent":["import * as BitsnapBackend from 'bitsnap-js/backend'\nimport type {\n CreateProductInput,\n ManagedProduct,\n ProductWriteResult,\n UpdateProductInput,\n} from 'bitsnap-js/backend'\n\nimport { DEFAULT_PRODUCTS_LIMIT } from '../constants.js'\n\nexport type ConfigureBitsnapBackendArgs = {\n /**\n * Bitsnap API key. Falls back to the key the plugin was configured with and\n * finally to `process.env.BITSNAP_API_KEY`.\n */\n apiKey?: string\n}\n\n/**\n * Raw envelope returned by the Bitsnap product list endpoint.\n */\nexport type ShopProductsResult = Awaited<ReturnType<typeof BitsnapBackend.getProducts>>\n\nlet configuredApiKey: string | null = null\n\n/**\n * Resolves the API key that should be used for the Bitsnap calls.\n *\n * Order: explicit argument → key the plugin was configured with →\n * `process.env.BITSNAP_API_KEY`.\n */\nexport function resolveBitsnapApiKey(apiKey?: string): string {\n if (typeof apiKey === 'string' && apiKey.length > 0) {\n return apiKey\n }\n\n if (typeof configuredApiKey === 'string' && configuredApiKey.length > 0) {\n return configuredApiKey\n }\n\n return process.env.BITSNAP_API_KEY ?? ''\n}\n\n/**\n * True when a Bitsnap API key is available. Drafts can only be listed with a key.\n */\nexport function isBitsnapApiKeyConfigured(apiKey?: string): boolean {\n return resolveBitsnapApiKey(apiKey).length > 0\n}\n\n/**\n * Points the Bitsnap SDK at the right host and gives it the API key.\n */\nexport function configureBitsnapBackend({ apiKey }: ConfigureBitsnapBackendArgs = {}): void {\n const resolvedApiKey = resolveBitsnapApiKey(apiKey)\n configuredApiKey = resolvedApiKey\n\n const customHost = process.env.CUSTOM_BITSNAP_HOST\n if (customHost != null && customHost.length > 0) {\n void BitsnapBackend.setCustomHost(customHost)\n }\n\n void BitsnapBackend.setApiKey(resolvedApiKey)\n}\n\n/**\n * Reads a single managed product (including its `revision`) from Bitsnap.\n */\nexport async function getManagedProduct(\n projectId: string,\n productId: string,\n): Promise<ManagedProduct> {\n configureBitsnapBackend()\n\n return BitsnapBackend.getManagedProduct(projectId, productId)\n}\n\n/**\n * Creates a managed product in Bitsnap.\n */\nexport async function createManagedProduct(\n projectId: string,\n input: CreateProductInput,\n): Promise<ProductWriteResult> {\n configureBitsnapBackend()\n\n return BitsnapBackend.createProduct(projectId, input)\n}\n\n/**\n * Patches a managed product in Bitsnap.\n */\nexport async function updateManagedProduct(\n projectId: string,\n productId: string,\n patch: UpdateProductInput,\n): Promise<ProductWriteResult> {\n configureBitsnapBackend()\n\n return BitsnapBackend.updateProduct(projectId, productId, patch)\n}\n\n/**\n * Lists the products of a Bitsnap project.\n *\n * Drafts are only included when an API key is configured — the public endpoint\n * does not serve them.\n */\nexport async function getShopProducts(\n projectId: string,\n limit: number = DEFAULT_PRODUCTS_LIMIT,\n): Promise<ShopProductsResult> {\n configureBitsnapBackend()\n\n const includeDrafts = isBitsnapApiKeyConfigured()\n\n return BitsnapBackend.getProducts(\n projectId,\n limit,\n 0,\n { cache: 'no-store' },\n { groupVariants: true, includeDrafts },\n )\n}\n"],"names":["BitsnapBackend","DEFAULT_PRODUCTS_LIMIT","configuredApiKey","resolveBitsnapApiKey","apiKey","length","process","env","BITSNAP_API_KEY","isBitsnapApiKeyConfigured","configureBitsnapBackend","resolvedApiKey","customHost","CUSTOM_BITSNAP_HOST","setCustomHost","setApiKey","getManagedProduct","projectId","productId","createManagedProduct","input","createProduct","updateManagedProduct","patch","updateProduct","getShopProducts","limit","includeDrafts","getProducts","cache","groupVariants"],"mappings":"AAAA,YAAYA,oBAAoB,qBAAoB;AAQpD,SAASC,sBAAsB,QAAQ,kBAAiB;AAexD,IAAIC,mBAAkC;AAEtC;;;;;CAKC,GACD,OAAO,SAASC,qBAAqBC,MAAe;IAClD,IAAI,OAAOA,WAAW,YAAYA,OAAOC,MAAM,GAAG,GAAG;QACnD,OAAOD;IACT;IAEA,IAAI,OAAOF,qBAAqB,YAAYA,iBAAiBG,MAAM,GAAG,GAAG;QACvE,OAAOH;IACT;IAEA,OAAOI,QAAQC,GAAG,CAACC,eAAe,IAAI;AACxC;AAEA;;CAEC,GACD,OAAO,SAASC,0BAA0BL,MAAe;IACvD,OAAOD,qBAAqBC,QAAQC,MAAM,GAAG;AAC/C;AAEA;;CAEC,GACD,OAAO,SAASK,wBAAwB,EAAEN,MAAM,EAA+B,GAAG,CAAC,CAAC;IAClF,MAAMO,iBAAiBR,qBAAqBC;IAC5CF,mBAAmBS;IAEnB,MAAMC,aAAaN,QAAQC,GAAG,CAACM,mBAAmB;IAClD,IAAID,cAAc,QAAQA,WAAWP,MAAM,GAAG,GAAG;QAC/C,KAAKL,eAAec,aAAa,CAACF;IACpC;IAEA,KAAKZ,eAAee,SAAS,CAACJ;AAChC;AAEA;;CAEC,GACD,OAAO,eAAeK,kBACpBC,SAAiB,EACjBC,SAAiB;IAEjBR;IAEA,OAAOV,eAAegB,iBAAiB,CAACC,WAAWC;AACrD;AAEA;;CAEC,GACD,OAAO,eAAeC,qBACpBF,SAAiB,EACjBG,KAAyB;IAEzBV;IAEA,OAAOV,eAAeqB,aAAa,CAACJ,WAAWG;AACjD;AAEA;;CAEC,GACD,OAAO,eAAeE,qBACpBL,SAAiB,EACjBC,SAAiB,EACjBK,KAAyB;IAEzBb;IAEA,OAAOV,eAAewB,aAAa,CAACP,WAAWC,WAAWK;AAC5D;AAEA;;;;;CAKC,GACD,OAAO,eAAeE,gBACpBR,SAAiB,EACjBS,QAAgBzB,sBAAsB;IAEtCS;IAEA,MAAMiB,gBAAgBlB;IAEtB,OAAOT,eAAe4B,WAAW,CAC/BX,WACAS,OACA,GACA;QAAEG,OAAO;IAAW,GACpB;QAAEC,eAAe;QAAMH;IAAc;AAEzC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Endpoint } from 'payload';
|
|
2
|
+
import type { SanitizedBitsnapPluginConfig } from '../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Builds the REST endpoints the admin UI talks to.
|
|
5
|
+
*
|
|
6
|
+
* They are mounted relative to Payload's API root, so with the default
|
|
7
|
+
* `apiBasePath` they are served at `/api/bitsnap/...`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function createBitsnapEndpoints(config: SanitizedBitsnapPluginConfig): Endpoint[];
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { configureBitsnapBackend, createManagedProduct, getManagedProduct, getShopProducts, isBitsnapApiKeyConfigured, updateManagedProduct } from './client.js';
|
|
2
|
+
import { bitsnapErrorResponse } from './errors.js';
|
|
3
|
+
import { bitsnapCreateSchema, bitsnapUpdateSchema, setBitsnapCurrency } from './schemas.js';
|
|
4
|
+
function unauthorized() {
|
|
5
|
+
return Response.json({
|
|
6
|
+
error: 'Brak autoryzacji'
|
|
7
|
+
}, {
|
|
8
|
+
status: 401
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function invalidBody() {
|
|
12
|
+
return Response.json({
|
|
13
|
+
error: 'Nieprawidłowe dane żądania'
|
|
14
|
+
}, {
|
|
15
|
+
status: 400
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
function missingProject() {
|
|
19
|
+
return Response.json({
|
|
20
|
+
error: 'Brak identyfikatora projektu Bitsnap'
|
|
21
|
+
}, {
|
|
22
|
+
status: 500
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function hasParams(req) {
|
|
26
|
+
return 'params' in req;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Reads the product id from the matched route. Payload puts path params into
|
|
30
|
+
* `req.routeParams`; `req.params` is accepted as a fallback.
|
|
31
|
+
*/ function getRouteId(req) {
|
|
32
|
+
const fromRouteParams = req.routeParams?.id;
|
|
33
|
+
if (typeof fromRouteParams === 'string' && fromRouteParams.length > 0) {
|
|
34
|
+
return fromRouteParams;
|
|
35
|
+
}
|
|
36
|
+
if (hasParams(req)) {
|
|
37
|
+
const fromParams = req.params?.id;
|
|
38
|
+
if (typeof fromParams === 'string' && fromParams.length > 0) {
|
|
39
|
+
return fromParams;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
async function readJsonBody(req) {
|
|
45
|
+
if (typeof req.json !== 'function') {
|
|
46
|
+
return {
|
|
47
|
+
ok: false
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
const data = await req.json();
|
|
52
|
+
return {
|
|
53
|
+
ok: true,
|
|
54
|
+
data
|
|
55
|
+
};
|
|
56
|
+
} catch {
|
|
57
|
+
return {
|
|
58
|
+
ok: false
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Builds the REST endpoints the admin UI talks to.
|
|
64
|
+
*
|
|
65
|
+
* They are mounted relative to Payload's API root, so with the default
|
|
66
|
+
* `apiBasePath` they are served at `/api/bitsnap/...`.
|
|
67
|
+
*/ export function createBitsnapEndpoints(config) {
|
|
68
|
+
setBitsnapCurrency(config.currency);
|
|
69
|
+
const createProduct = async (req)=>{
|
|
70
|
+
if (!req.user) {
|
|
71
|
+
return unauthorized();
|
|
72
|
+
}
|
|
73
|
+
if (!config.projectId) {
|
|
74
|
+
return missingProject();
|
|
75
|
+
}
|
|
76
|
+
const body = await readJsonBody(req);
|
|
77
|
+
if (!body.ok) {
|
|
78
|
+
return invalidBody();
|
|
79
|
+
}
|
|
80
|
+
const parsed = bitsnapCreateSchema.safeParse(body.data);
|
|
81
|
+
if (!parsed.success) {
|
|
82
|
+
return invalidBody();
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
configureBitsnapBackend({
|
|
86
|
+
apiKey: config.bitsnapApiKey
|
|
87
|
+
});
|
|
88
|
+
const result = await createManagedProduct(config.projectId, parsed.data);
|
|
89
|
+
return Response.json(result);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
return bitsnapErrorResponse(error);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const readProduct = async (req)=>{
|
|
95
|
+
if (!req.user) {
|
|
96
|
+
return unauthorized();
|
|
97
|
+
}
|
|
98
|
+
if (!config.projectId) {
|
|
99
|
+
return missingProject();
|
|
100
|
+
}
|
|
101
|
+
const id = getRouteId(req);
|
|
102
|
+
if (!id) {
|
|
103
|
+
return invalidBody();
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
configureBitsnapBackend({
|
|
107
|
+
apiKey: config.bitsnapApiKey
|
|
108
|
+
});
|
|
109
|
+
const product = await getManagedProduct(config.projectId, id);
|
|
110
|
+
return Response.json({
|
|
111
|
+
product
|
|
112
|
+
});
|
|
113
|
+
} catch (error) {
|
|
114
|
+
return bitsnapErrorResponse(error);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const updateProduct = async (req)=>{
|
|
118
|
+
if (!req.user) {
|
|
119
|
+
return unauthorized();
|
|
120
|
+
}
|
|
121
|
+
if (!config.projectId) {
|
|
122
|
+
return missingProject();
|
|
123
|
+
}
|
|
124
|
+
const id = getRouteId(req);
|
|
125
|
+
if (!id) {
|
|
126
|
+
return invalidBody();
|
|
127
|
+
}
|
|
128
|
+
const body = await readJsonBody(req);
|
|
129
|
+
if (!body.ok) {
|
|
130
|
+
return invalidBody();
|
|
131
|
+
}
|
|
132
|
+
const parsed = bitsnapUpdateSchema.safeParse(body.data);
|
|
133
|
+
if (!parsed.success) {
|
|
134
|
+
return invalidBody();
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
configureBitsnapBackend({
|
|
138
|
+
apiKey: config.bitsnapApiKey
|
|
139
|
+
});
|
|
140
|
+
const result = await updateManagedProduct(config.projectId, id, parsed.data);
|
|
141
|
+
return Response.json(result);
|
|
142
|
+
} catch (error) {
|
|
143
|
+
return bitsnapErrorResponse(error);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
const listShopProducts = async (req)=>{
|
|
147
|
+
if (!req.user) {
|
|
148
|
+
return unauthorized();
|
|
149
|
+
}
|
|
150
|
+
if (!config.projectId) {
|
|
151
|
+
return missingProject();
|
|
152
|
+
}
|
|
153
|
+
configureBitsnapBackend({
|
|
154
|
+
apiKey: config.bitsnapApiKey
|
|
155
|
+
});
|
|
156
|
+
if (!isBitsnapApiKeyConfigured()) {
|
|
157
|
+
req.payload.logger.warn('Bitsnap: BITSNAP_API_KEY is not set — the product picker lists published products only');
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
const result = await getShopProducts(config.projectId);
|
|
161
|
+
return Response.json(result);
|
|
162
|
+
} catch (error) {
|
|
163
|
+
return bitsnapErrorResponse(error);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
return [
|
|
167
|
+
{
|
|
168
|
+
handler: createProduct,
|
|
169
|
+
method: 'post',
|
|
170
|
+
path: `${config.apiBasePath}/products`
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
handler: readProduct,
|
|
174
|
+
method: 'get',
|
|
175
|
+
path: `${config.apiBasePath}/products/:id`
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
handler: updateProduct,
|
|
179
|
+
method: 'patch',
|
|
180
|
+
path: `${config.apiBasePath}/products/:id`
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
handler: listShopProducts,
|
|
184
|
+
method: 'get',
|
|
185
|
+
path: `${config.apiBasePath}/shop-products`
|
|
186
|
+
}
|
|
187
|
+
];
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
//# sourceMappingURL=endpoints.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/server/endpoints.ts"],"sourcesContent":["import type { Endpoint, PayloadRequest } from 'payload'\n\nimport type { SanitizedBitsnapPluginConfig } from '../types.js'\nimport {\n configureBitsnapBackend,\n createManagedProduct,\n getManagedProduct,\n getShopProducts,\n isBitsnapApiKeyConfigured,\n updateManagedProduct,\n} from './client.js'\nimport { bitsnapErrorResponse } from './errors.js'\nimport { bitsnapCreateSchema, bitsnapUpdateSchema, setBitsnapCurrency } from './schemas.js'\n\ntype RequestWithParams = PayloadRequest & {\n params?: {\n id?: unknown\n }\n}\n\ntype BodyResult = {\n ok: true\n data: unknown\n} | {\n ok: false\n}\n\nfunction unauthorized(): Response {\n return Response.json({ error: 'Brak autoryzacji' }, { status: 401 })\n}\n\nfunction invalidBody(): Response {\n return Response.json({ error: 'Nieprawidłowe dane żądania' }, { status: 400 })\n}\n\nfunction missingProject(): Response {\n return Response.json({ error: 'Brak identyfikatora projektu Bitsnap' }, { status: 500 })\n}\n\nfunction hasParams(req: PayloadRequest): req is RequestWithParams {\n return 'params' in req\n}\n\n/**\n * Reads the product id from the matched route. Payload puts path params into\n * `req.routeParams`; `req.params` is accepted as a fallback.\n */\nfunction getRouteId(req: PayloadRequest): string | null {\n const fromRouteParams = req.routeParams?.id\n if (typeof fromRouteParams === 'string' && fromRouteParams.length > 0) {\n return fromRouteParams\n }\n\n if (hasParams(req)) {\n const fromParams = req.params?.id\n if (typeof fromParams === 'string' && fromParams.length > 0) {\n return fromParams\n }\n }\n\n return null\n}\n\nasync function readJsonBody(req: PayloadRequest): Promise<BodyResult> {\n if (typeof req.json !== 'function') {\n return { ok: false }\n }\n\n try {\n const data = await req.json()\n\n return { ok: true, data }\n } catch {\n return { ok: false }\n }\n}\n\n/**\n * Builds the REST endpoints the admin UI talks to.\n *\n * They are mounted relative to Payload's API root, so with the default\n * `apiBasePath` they are served at `/api/bitsnap/...`.\n */\nexport function createBitsnapEndpoints(config: SanitizedBitsnapPluginConfig): Endpoint[] {\n setBitsnapCurrency(config.currency)\n\n const createProduct = async (req: PayloadRequest): Promise<Response> => {\n if (!req.user) {\n return unauthorized()\n }\n\n if (!config.projectId) {\n return missingProject()\n }\n\n const body = await readJsonBody(req)\n if (!body.ok) {\n return invalidBody()\n }\n\n const parsed = bitsnapCreateSchema.safeParse(body.data)\n if (!parsed.success) {\n return invalidBody()\n }\n\n try {\n configureBitsnapBackend({ apiKey: config.bitsnapApiKey })\n\n const result = await createManagedProduct(config.projectId, parsed.data)\n\n return Response.json(result)\n } catch (error) {\n return bitsnapErrorResponse(error)\n }\n }\n\n const readProduct = async (req: PayloadRequest): Promise<Response> => {\n if (!req.user) {\n return unauthorized()\n }\n\n if (!config.projectId) {\n return missingProject()\n }\n\n const id = getRouteId(req)\n if (!id) {\n return invalidBody()\n }\n\n try {\n configureBitsnapBackend({ apiKey: config.bitsnapApiKey })\n\n const product = await getManagedProduct(config.projectId, id)\n\n return Response.json({ product })\n } catch (error) {\n return bitsnapErrorResponse(error)\n }\n }\n\n const updateProduct = async (req: PayloadRequest): Promise<Response> => {\n if (!req.user) {\n return unauthorized()\n }\n\n if (!config.projectId) {\n return missingProject()\n }\n\n const id = getRouteId(req)\n if (!id) {\n return invalidBody()\n }\n\n const body = await readJsonBody(req)\n if (!body.ok) {\n return invalidBody()\n }\n\n const parsed = bitsnapUpdateSchema.safeParse(body.data)\n if (!parsed.success) {\n return invalidBody()\n }\n\n try {\n configureBitsnapBackend({ apiKey: config.bitsnapApiKey })\n\n const result = await updateManagedProduct(config.projectId, id, parsed.data)\n\n return Response.json(result)\n } catch (error) {\n return bitsnapErrorResponse(error)\n }\n }\n\n const listShopProducts = async (req: PayloadRequest): Promise<Response> => {\n if (!req.user) {\n return unauthorized()\n }\n\n if (!config.projectId) {\n return missingProject()\n }\n\n configureBitsnapBackend({ apiKey: config.bitsnapApiKey })\n\n if (!isBitsnapApiKeyConfigured()) {\n req.payload.logger.warn(\n 'Bitsnap: BITSNAP_API_KEY is not set — the product picker lists published products only',\n )\n }\n\n try {\n const result = await getShopProducts(config.projectId)\n\n return Response.json(result)\n } catch (error) {\n return bitsnapErrorResponse(error)\n }\n }\n\n return [\n {\n handler: createProduct,\n method: 'post',\n path: `${config.apiBasePath}/products`,\n },\n {\n handler: readProduct,\n method: 'get',\n path: `${config.apiBasePath}/products/:id`,\n },\n {\n handler: updateProduct,\n method: 'patch',\n path: `${config.apiBasePath}/products/:id`,\n },\n {\n handler: listShopProducts,\n method: 'get',\n path: `${config.apiBasePath}/shop-products`,\n },\n ]\n}\n"],"names":["configureBitsnapBackend","createManagedProduct","getManagedProduct","getShopProducts","isBitsnapApiKeyConfigured","updateManagedProduct","bitsnapErrorResponse","bitsnapCreateSchema","bitsnapUpdateSchema","setBitsnapCurrency","unauthorized","Response","json","error","status","invalidBody","missingProject","hasParams","req","getRouteId","fromRouteParams","routeParams","id","length","fromParams","params","readJsonBody","ok","data","createBitsnapEndpoints","config","currency","createProduct","user","projectId","body","parsed","safeParse","success","apiKey","bitsnapApiKey","result","readProduct","product","updateProduct","listShopProducts","payload","logger","warn","handler","method","path","apiBasePath"],"mappings":"AAGA,SACEA,uBAAuB,EACvBC,oBAAoB,EACpBC,iBAAiB,EACjBC,eAAe,EACfC,yBAAyB,EACzBC,oBAAoB,QACf,cAAa;AACpB,SAASC,oBAAoB,QAAQ,cAAa;AAClD,SAASC,mBAAmB,EAAEC,mBAAmB,EAAEC,kBAAkB,QAAQ,eAAc;AAe3F,SAASC;IACP,OAAOC,SAASC,IAAI,CAAC;QAAEC,OAAO;IAAmB,GAAG;QAAEC,QAAQ;IAAI;AACpE;AAEA,SAASC;IACP,OAAOJ,SAASC,IAAI,CAAC;QAAEC,OAAO;IAA6B,GAAG;QAAEC,QAAQ;IAAI;AAC9E;AAEA,SAASE;IACP,OAAOL,SAASC,IAAI,CAAC;QAAEC,OAAO;IAAuC,GAAG;QAAEC,QAAQ;IAAI;AACxF;AAEA,SAASG,UAAUC,GAAmB;IACpC,OAAO,YAAYA;AACrB;AAEA;;;CAGC,GACD,SAASC,WAAWD,GAAmB;IACrC,MAAME,kBAAkBF,IAAIG,WAAW,EAAEC;IACzC,IAAI,OAAOF,oBAAoB,YAAYA,gBAAgBG,MAAM,GAAG,GAAG;QACrE,OAAOH;IACT;IAEA,IAAIH,UAAUC,MAAM;QAClB,MAAMM,aAAaN,IAAIO,MAAM,EAAEH;QAC/B,IAAI,OAAOE,eAAe,YAAYA,WA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C,IAAI,CAAC6B;QACvB,EAAE,OAAO5B,OAAO;YACd,OAAOP,qBAAqBO;QAC9B;IACF;IAEA,OAAO;QACL;YACEoC,SAASjB;YACTkB,QAAQ;YACRC,MAAM,GAAGrB,OAAOsB,WAAW,CAAC,SAAS,CAAC;QACxC;QACA;YACEH,SAASP;YACTQ,QAAQ;YACRC,MAAM,GAAGrB,OAAOsB,WAAW,CAAC,aAAa,CAAC;QAC5C;QACA;YACEH,SAASL;YACTM,QAAQ;YACRC,MAAM,GAAGrB,OAAOsB,WAAW,CAAC,aAAa,CAAC;QAC5C;QACA;YACEH,SAASJ;YACTK,QAAQ;YACRC,MAAM,GAAGrB,OAAOsB,WAAW,CAAC,cAAc,CAAC;QAC7C;KACD;AACH"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BitsnapApiError } from 'bitsnap-js/errors';
|
|
2
|
+
/**
|
|
3
|
+
* Maps a Bitsnap API error onto the message shown in the Payload admin.
|
|
4
|
+
*/
|
|
5
|
+
export declare function bitsnapErrorMessage(error: BitsnapApiError): string;
|
|
6
|
+
/**
|
|
7
|
+
* Turns any error thrown by the Bitsnap SDK into a JSON response.
|
|
8
|
+
*/
|
|
9
|
+
export declare function bitsnapErrorResponse(error: unknown): Response;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { BitsnapApiError } from 'bitsnap-js/errors';
|
|
2
|
+
/**
|
|
3
|
+
* Maps a Bitsnap API error onto the message shown in the Payload admin.
|
|
4
|
+
*/ export function bitsnapErrorMessage(error) {
|
|
5
|
+
switch(error.code){
|
|
6
|
+
case 'REVISION_CONFLICT':
|
|
7
|
+
return 'Produkt zmienił się w Bitsnapie — kliknij Odśwież';
|
|
8
|
+
case 'PRODUCT_NOT_FOUND':
|
|
9
|
+
return 'Nie znaleziono produktu w Bitsnapie';
|
|
10
|
+
case 'MISSING_SCOPE':
|
|
11
|
+
return 'Klucz API Bitsnapa nie ma uprawnień do zarządzania produktami';
|
|
12
|
+
default:
|
|
13
|
+
return `Bitsnap odrzucił żądanie (${error.status})`;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Turns any error thrown by the Bitsnap SDK into a JSON response.
|
|
18
|
+
*/ export function bitsnapErrorResponse(error) {
|
|
19
|
+
if (error instanceof BitsnapApiError) {
|
|
20
|
+
return Response.json({
|
|
21
|
+
error: bitsnapErrorMessage(error),
|
|
22
|
+
code: error.code
|
|
23
|
+
}, {
|
|
24
|
+
status: error.status
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
console.error('Bitsnap request failed:', error);
|
|
28
|
+
return Response.json({
|
|
29
|
+
error: 'Nie udało się połączyć z Bitsnapem'
|
|
30
|
+
}, {
|
|
31
|
+
status: 502
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/server/errors.ts"],"sourcesContent":["import { BitsnapApiError } from 'bitsnap-js/errors'\n\n/**\n * Maps a Bitsnap API error onto the message shown in the Payload admin.\n */\nexport function bitsnapErrorMessage(error: BitsnapApiError): string {\n switch (error.code) {\n case 'REVISION_CONFLICT':\n return 'Produkt zmienił się w Bitsnapie — kliknij Odśwież'\n case 'PRODUCT_NOT_FOUND':\n return 'Nie znaleziono produktu w Bitsnapie'\n case 'MISSING_SCOPE':\n return 'Klucz API Bitsnapa nie ma uprawnień do zarządzania produktami'\n default:\n return `Bitsnap odrzucił żądanie (${error.status})`\n }\n}\n\n/**\n * Turns any error thrown by the Bitsnap SDK into a JSON response.\n */\nexport function bitsnapErrorResponse(error: unknown): Response {\n if (error instanceof BitsnapApiError) {\n return Response.json(\n { error: bitsnapErrorMessage(error), code: error.code },\n { status: error.status },\n )\n }\n\n console.error('Bitsnap request failed:', error)\n\n return Response.json({ error: 'Nie udało się połączyć z Bitsnapem' }, { status: 502 })\n}\n"],"names":["BitsnapApiError","bitsnapErrorMessage","error","code","status","bitsnapErrorResponse","Response","json","console"],"mappings":"AAAA,SAASA,eAAe,QAAQ,oBAAmB;AAEnD;;CAEC,GACD,OAAO,SAASC,oBAAoBC,KAAsB;IACxD,OAAQA,MAAMC,IAAI;QAChB,KAAK;YACH,OAAO;QACT,KAAK;YACH,OAAO;QACT,KAAK;YACH,OAAO;QACT;YACE,OAAO,CAAC,0BAA0B,EAAED,MAAME,MAAM,CAAC,CAAC,CAAC;IACvD;AACF;AAEA;;CAEC,GACD,OAAO,SAASC,qBAAqBH,KAAc;IACjD,IAAIA,iBAAiBF,iBAAiB;QACpC,OAAOM,SAASC,IAAI,CAClB;YAAEL,OAAOD,oBAAoBC;YAAQC,MAAMD,MAAMC,IAAI;QAAC,GACtD;YAAEC,QAAQF,MAAME,MAAM;QAAC;IAE3B;IAEAI,QAAQN,KAAK,CAAC,2BAA2BA;IAEzC,OAAOI,SAASC,IAAI,CAAC;QAAEL,OAAO;IAAqC,GAAG;QAAEE,QAAQ;IAAI;AACtF"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { configureBitsnapBackend, createManagedProduct, getManagedProduct, getShopProducts, isBitsnapApiKeyConfigured, resolveBitsnapApiKey, updateManagedProduct, } from './client.js';
|
|
2
|
+
export type { ConfigureBitsnapBackendArgs, ShopProductsResult, } from './client.js';
|
|
3
|
+
export { bitsnapErrorMessage, bitsnapErrorResponse } from './errors.js';
|
|
4
|
+
export { createBitsnapEndpoints } from './endpoints.js';
|
|
5
|
+
export { bitsnapCreateSchema, bitsnapStatusSchema, bitsnapUpdateSchema, bitsnapVariantSchema, getBitsnapCurrency, setBitsnapCurrency, } from './schemas.js';
|
|
6
|
+
export type { BitsnapCreateInput, BitsnapStatus, BitsnapUpdateInput, BitsnapVariantInput, } from './schemas.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { configureBitsnapBackend, createManagedProduct, getManagedProduct, getShopProducts, isBitsnapApiKeyConfigured, resolveBitsnapApiKey, updateManagedProduct } from './client.js';
|
|
2
|
+
export { bitsnapErrorMessage, bitsnapErrorResponse } from './errors.js';
|
|
3
|
+
export { createBitsnapEndpoints } from './endpoints.js';
|
|
4
|
+
export { bitsnapCreateSchema, bitsnapStatusSchema, bitsnapUpdateSchema, bitsnapVariantSchema, getBitsnapCurrency, setBitsnapCurrency } from './schemas.js';
|
|
5
|
+
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/server/index.ts"],"sourcesContent":["export {\n configureBitsnapBackend,\n createManagedProduct,\n getManagedProduct,\n getShopProducts,\n isBitsnapApiKeyConfigured,\n resolveBitsnapApiKey,\n updateManagedProduct,\n} from './client.js'\nexport type {\n ConfigureBitsnapBackendArgs,\n ShopProductsResult,\n} from './client.js'\nexport { bitsnapErrorMessage, bitsnapErrorResponse } from './errors.js'\nexport { createBitsnapEndpoints } from './endpoints.js'\nexport {\n bitsnapCreateSchema,\n bitsnapStatusSchema,\n bitsnapUpdateSchema,\n bitsnapVariantSchema,\n getBitsnapCurrency,\n setBitsnapCurrency,\n} from './schemas.js'\nexport type {\n BitsnapCreateInput,\n BitsnapStatus,\n BitsnapUpdateInput,\n BitsnapVariantInput,\n} from './schemas.js'\n"],"names":["configureBitsnapBackend","createManagedProduct","getManagedProduct","getShopProducts","isBitsnapApiKeyConfigured","resolveBitsnapApiKey","updateManagedProduct","bitsnapErrorMessage","bitsnapErrorResponse","createBitsnapEndpoints","bitsnapCreateSchema","bitsnapStatusSchema","bitsnapUpdateSchema","bitsnapVariantSchema","getBitsnapCurrency","setBitsnapCurrency"],"mappings":"AAAA,SACEA,uBAAuB,EACvBC,oBAAoB,EACpBC,iBAAiB,EACjBC,eAAe,EACfC,yBAAyB,EACzBC,oBAAoB,EACpBC,oBAAoB,QACf,cAAa;AAKpB,SAASC,mBAAmB,EAAEC,oBAAoB,QAAQ,cAAa;AACvE,SAASC,sBAAsB,QAAQ,iBAAgB;AACvD,SACEC,mBAAmB,EACnBC,mBAAmB,EACnBC,mBAAmB,EACnBC,oBAAoB,EACpBC,kBAAkB,EAClBC,kBAAkB,QACb,eAAc"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare function setBitsnapCurrency(currency: string): void;
|
|
3
|
+
export declare function getBitsnapCurrency(): string;
|
|
4
|
+
export declare const bitsnapStatusSchema: z.ZodEnum<{
|
|
5
|
+
DRAFT: "DRAFT";
|
|
6
|
+
UNPUBLISHED: "UNPUBLISHED";
|
|
7
|
+
PUBLISHED: "PUBLISHED";
|
|
8
|
+
}>;
|
|
9
|
+
export declare const bitsnapVariantSchema: z.ZodObject<{
|
|
10
|
+
id: z.ZodOptional<z.ZodString>;
|
|
11
|
+
name: z.ZodString;
|
|
12
|
+
priceAmount: z.ZodNumber;
|
|
13
|
+
priceCurrency: z.ZodOptional<z.ZodString>;
|
|
14
|
+
availableQuantity: z.ZodOptional<z.ZodNumber>;
|
|
15
|
+
}, z.core.$strip>;
|
|
16
|
+
export declare const bitsnapCreateSchema: z.ZodObject<{
|
|
17
|
+
name: z.ZodString;
|
|
18
|
+
priceAmount: z.ZodNumber;
|
|
19
|
+
priceCurrency: z.ZodDefault<z.ZodString>;
|
|
20
|
+
status: z.ZodOptional<z.ZodEnum<{
|
|
21
|
+
DRAFT: "DRAFT";
|
|
22
|
+
UNPUBLISHED: "UNPUBLISHED";
|
|
23
|
+
PUBLISHED: "PUBLISHED";
|
|
24
|
+
}>>;
|
|
25
|
+
availableQuantity: z.ZodOptional<z.ZodNumber>;
|
|
26
|
+
variants: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
27
|
+
id: z.ZodOptional<z.ZodString>;
|
|
28
|
+
name: z.ZodString;
|
|
29
|
+
priceAmount: z.ZodNumber;
|
|
30
|
+
priceCurrency: z.ZodOptional<z.ZodString>;
|
|
31
|
+
availableQuantity: z.ZodOptional<z.ZodNumber>;
|
|
32
|
+
}, z.core.$strip>>>;
|
|
33
|
+
}, z.core.$strip>;
|
|
34
|
+
export declare const bitsnapUpdateSchema: z.ZodObject<{
|
|
35
|
+
revision: z.ZodOptional<z.ZodNumber>;
|
|
36
|
+
name: z.ZodOptional<z.ZodString>;
|
|
37
|
+
priceAmount: z.ZodOptional<z.ZodNumber>;
|
|
38
|
+
status: z.ZodOptional<z.ZodEnum<{
|
|
39
|
+
DRAFT: "DRAFT";
|
|
40
|
+
UNPUBLISHED: "UNPUBLISHED";
|
|
41
|
+
PUBLISHED: "PUBLISHED";
|
|
42
|
+
}>>;
|
|
43
|
+
availableQuantity: z.ZodOptional<z.ZodNumber>;
|
|
44
|
+
variants: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
45
|
+
id: z.ZodOptional<z.ZodString>;
|
|
46
|
+
name: z.ZodString;
|
|
47
|
+
priceAmount: z.ZodNumber;
|
|
48
|
+
priceCurrency: z.ZodOptional<z.ZodString>;
|
|
49
|
+
availableQuantity: z.ZodOptional<z.ZodNumber>;
|
|
50
|
+
}, z.core.$strip>>>;
|
|
51
|
+
}, z.core.$strip>;
|
|
52
|
+
export type BitsnapStatus = z.infer<typeof bitsnapStatusSchema>;
|
|
53
|
+
export type BitsnapVariantInput = z.infer<typeof bitsnapVariantSchema>;
|
|
54
|
+
export type BitsnapCreateInput = z.infer<typeof bitsnapCreateSchema>;
|
|
55
|
+
export type BitsnapUpdateInput = z.infer<typeof bitsnapUpdateSchema>;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { DEFAULT_CURRENCY } from '../constants.js';
|
|
3
|
+
/**
|
|
4
|
+
* Currency used when the request body omits `priceCurrency`.
|
|
5
|
+
*
|
|
6
|
+
* The plugin sets it from its own configuration during initialization, so the
|
|
7
|
+
* default follows the project instead of being hardcoded.
|
|
8
|
+
*/ let currentCurrency = DEFAULT_CURRENCY;
|
|
9
|
+
export function setBitsnapCurrency(currency) {
|
|
10
|
+
if (typeof currency === 'string' && currency.length > 0) {
|
|
11
|
+
currentCurrency = currency;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function getBitsnapCurrency() {
|
|
15
|
+
return currentCurrency;
|
|
16
|
+
}
|
|
17
|
+
export const bitsnapStatusSchema = z.enum([
|
|
18
|
+
'DRAFT',
|
|
19
|
+
'UNPUBLISHED',
|
|
20
|
+
'PUBLISHED'
|
|
21
|
+
]);
|
|
22
|
+
export const bitsnapVariantSchema = z.object({
|
|
23
|
+
id: z.string().optional(),
|
|
24
|
+
name: z.string().min(1),
|
|
25
|
+
priceAmount: z.number().int().nonnegative(),
|
|
26
|
+
priceCurrency: z.string().min(1).optional(),
|
|
27
|
+
availableQuantity: z.number().int().nonnegative().optional()
|
|
28
|
+
});
|
|
29
|
+
export const bitsnapCreateSchema = z.object({
|
|
30
|
+
name: z.string().min(1),
|
|
31
|
+
priceAmount: z.number().int().nonnegative(),
|
|
32
|
+
priceCurrency: z.string().min(1).default(()=>getBitsnapCurrency()),
|
|
33
|
+
status: bitsnapStatusSchema.optional(),
|
|
34
|
+
availableQuantity: z.number().int().nonnegative().optional(),
|
|
35
|
+
variants: z.array(bitsnapVariantSchema).optional()
|
|
36
|
+
});
|
|
37
|
+
export const bitsnapUpdateSchema = z.object({
|
|
38
|
+
revision: z.number().int().optional(),
|
|
39
|
+
name: z.string().min(1).optional(),
|
|
40
|
+
priceAmount: z.number().int().nonnegative().optional(),
|
|
41
|
+
status: bitsnapStatusSchema.optional(),
|
|
42
|
+
availableQuantity: z.number().int().nonnegative().optional(),
|
|
43
|
+
variants: z.array(bitsnapVariantSchema).optional()
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
//# sourceMappingURL=schemas.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/server/schemas.ts"],"sourcesContent":["import { z } from 'zod'\n\nimport { DEFAULT_CURRENCY } from '../constants.js'\n\n/**\n * Currency used when the request body omits `priceCurrency`.\n *\n * The plugin sets it from its own configuration during initialization, so the\n * default follows the project instead of being hardcoded.\n */\nlet currentCurrency: string = DEFAULT_CURRENCY\n\nexport function setBitsnapCurrency(currency: string): void {\n if (typeof currency === 'string' && currency.length > 0) {\n currentCurrency = currency\n }\n}\n\nexport function getBitsnapCurrency(): string {\n return currentCurrency\n}\n\nexport const bitsnapStatusSchema = z.enum(['DRAFT', 'UNPUBLISHED', 'PUBLISHED'])\n\nexport const bitsnapVariantSchema = z.object({\n id: z.string().optional(),\n name: z.string().min(1),\n priceAmount: z.number().int().nonnegative(),\n priceCurrency: z.string().min(1).optional(),\n availableQuantity: z.number().int().nonnegative().optional(),\n})\n\nexport const bitsnapCreateSchema = z.object({\n name: z.string().min(1),\n priceAmount: z.number().int().nonnegative(),\n priceCurrency: z.string().min(1).default(() => getBitsnapCurrency()),\n status: bitsnapStatusSchema.optional(),\n availableQuantity: z.number().int().nonnegative().optional(),\n variants: z.array(bitsnapVariantSchema).optional(),\n})\n\nexport const bitsnapUpdateSchema = z.object({\n revision: z.number().int().optional(),\n name: z.string().min(1).optional(),\n priceAmount: z.number().int().nonnegative().optional(),\n status: bitsnapStatusSchema.optional(),\n availableQuantity: z.number().int().nonnegative().optional(),\n variants: z.array(bitsnapVariantSchema).optional(),\n})\n\nexport type BitsnapStatus = z.infer<typeof bitsnapStatusSchema>\nexport type BitsnapVariantInput = z.infer<typeof bitsnapVariantSchema>\nexport type BitsnapCreateInput = z.infer<typeof bitsnapCreateSchema>\nexport type BitsnapUpdateInput = z.infer<typeof bitsnapUpdateSchema>\n"],"names":["z","DEFAULT_CURRENCY","currentCurrency","setBitsnapCurrency","currency","length","getBitsnapCurrency","bitsnapStatusSchema","enum","bitsnapVariantSchema","object","id","string","optional","name","min","priceAmount","number","int","nonnegative","priceCurrency","availableQuantity","bitsnapCreateSchema","default","status","variants","array","bitsnapUpdateSchema","revision"],"mappings":"AAAA,SAASA,CAAC,QAAQ,MAAK;AAEvB,SAASC,gBAAgB,QAAQ,kBAAiB;A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