@decocms/apps-magento 8.1.0-next.1 → 8.1.0-next.2

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.
@@ -0,0 +1,2 @@
1
+ export { createMagentoClient, type MagentoClient, type MagentoClientConfig, type MagentoClientOptions, MagentoError, type MagentoRequestInit, } from "./magentoClient.ts";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EACnB,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,EACzB,YAAY,EACZ,KAAK,kBAAkB,GACxB,MAAM,oBAAoB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ // `@decocms/apps-magento`: the thin Magento client (see /next/upstream-clients).
2
+ export { createMagentoClient, MagentoError, } from "./magentoClient.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,iFAAiF;AACjF,OAAO,EACL,mBAAmB,EAInB,YAAY,GAEb,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,62 @@
1
+ /**
2
+ * The Magento client: a thin client for a Magento store's REST and GraphQL
3
+ * APIs, over the framework's instrumented fetch (provider "magento"). See
4
+ * /next/upstream-clients.
5
+ *
6
+ * Configuration comes in as arguments; the site reads its environment (or a
7
+ * `secret` block) where it creates the client. Converters, hooks, cart and
8
+ * session flows, caching and feature toggles belong to the site (platform
9
+ * templates), not here.
10
+ */
11
+ import { createInstrumentedFetch } from "@decocms/blocks/fetch";
12
+ export interface MagentoClientConfig {
13
+ /** The store's origin, e.g. `https://store.example.com`. Every request goes here. */
14
+ baseUrl: string;
15
+ /** Integration access token, sent as `Authorization: Bearer` unless a call opts out. */
16
+ apiKey?: string;
17
+ /** Optional value sent as `x-origin-header`, for stores behind an origin guard. */
18
+ originHeader?: string;
19
+ }
20
+ export interface MagentoClientOptions {
21
+ /** The fetch underneath, e.g. a fake in tests. Defaults to `globalThis.fetch`. */
22
+ fetch?: Parameters<typeof createInstrumentedFetch>[0]["fetch"];
23
+ }
24
+ export interface MagentoRequestInit extends RequestInit {
25
+ /** The operation label, the API's own name for the call (e.g. `getCart`), never a URL. */
26
+ operation: string;
27
+ /**
28
+ * Send the `apiKey` bearer token. Default true for `request`/`rest`, false
29
+ * for `graphql`. Never replaces an `Authorization` header the call sets
30
+ * (e.g. a customer token).
31
+ */
32
+ authenticated?: boolean;
33
+ }
34
+ /** Thrown on a non-2xx response or a GraphQL `errors` payload. Never carries bodies or tokens. */
35
+ export declare class MagentoError extends Error {
36
+ readonly operation: string;
37
+ readonly status: number;
38
+ /** How many GraphQL errors the response carried (0 for an HTTP failure). */
39
+ readonly graphqlErrors: number;
40
+ constructor(operation: string, status: number,
41
+ /** How many GraphQL errors the response carried (0 for an HTTP failure). */
42
+ graphqlErrors?: number);
43
+ }
44
+ export type MagentoClient = ReturnType<typeof createMagentoClient>;
45
+ export declare function createMagentoClient(config: MagentoClientConfig, options?: MagentoClientOptions): {
46
+ request: (path: string, init: MagentoRequestInit) => Promise<Response>;
47
+ /** A REST call (`/rest/<store>/V1/...`) returning its parsed JSON body. */
48
+ rest<T>(path: string, init: MagentoRequestInit): Promise<T>;
49
+ /**
50
+ * A GraphQL operation on `/graphql`. `operationName` is sent with the
51
+ * request and is the operation label. `headers` adds per-call headers,
52
+ * such as `Store` or a customer `Authorization` token. Storefront
53
+ * GraphQL is public, so the `apiKey` is sent only with
54
+ * `authenticated: true`, and never over a customer token.
55
+ */
56
+ graphql<TData, TVariables = Record<string, unknown>>(query: string, variables: TVariables | undefined, init: {
57
+ operationName: string;
58
+ headers?: Record<string, string>;
59
+ authenticated?: boolean;
60
+ }): Promise<TData>;
61
+ };
62
+ //# sourceMappingURL=magentoClient.d.ts.map
@@ -0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M,aACF,UAAU,GAAG,SAAS,QAC3B;QAAE,aAAa,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAAC,aAAa,CAAC,EAAE,OAAO,CAAA;KAAE,GACzF,OAAO,CAAC,KAAK,CAAC;EAoBpB"}
@@ -0,0 +1,96 @@
1
+ /**
2
+ * The Magento client: a thin client for a Magento store's REST and GraphQL
3
+ * APIs, over the framework's instrumented fetch (provider "magento"). See
4
+ * /next/upstream-clients.
5
+ *
6
+ * Configuration comes in as arguments; the site reads its environment (or a
7
+ * `secret` block) where it creates the client. Converters, hooks, cart and
8
+ * session flows, caching and feature toggles belong to the site (platform
9
+ * templates), not here.
10
+ */
11
+ import { createInstrumentedFetch } from "@decocms/blocks/fetch";
12
+ /** Thrown on a non-2xx response or a GraphQL `errors` payload. Never carries bodies or tokens. */
13
+ export class MagentoError extends Error {
14
+ operation;
15
+ status;
16
+ graphqlErrors;
17
+ constructor(operation, status,
18
+ /** How many GraphQL errors the response carried (0 for an HTTP failure). */
19
+ graphqlErrors = 0) {
20
+ super(graphqlErrors > 0
21
+ ? `magento ${operation} returned ${graphqlErrors} GraphQL error(s)`
22
+ : `magento ${operation} failed with HTTP ${status}`);
23
+ this.operation = operation;
24
+ this.status = status;
25
+ this.graphqlErrors = graphqlErrors;
26
+ this.name = "MagentoError";
27
+ }
28
+ }
29
+ export function createMagentoClient(config, options = {}) {
30
+ const instrumented = createInstrumentedFetch({ provider: "magento", fetch: options.fetch });
31
+ const origin = new URL(config.baseUrl).origin;
32
+ /**
33
+ * A request to a path on the store, e.g. `/rest/default/V1/carts/mine` or
34
+ * `/customer/section/load`, returning the raw response. Paths only: the
35
+ * store's credentials never go to another origin.
36
+ */
37
+ async function request(path, init) {
38
+ const { operation, authenticated = true, ...rest } = init;
39
+ const url = new URL(path, origin);
40
+ if (url.origin !== origin) {
41
+ throw new Error(`magento ${operation}: only paths on the configured store are allowed`);
42
+ }
43
+ const headers = new Headers(rest.headers);
44
+ if (authenticated && config.apiKey && !headers.has("authorization")) {
45
+ headers.set("authorization", `Bearer ${config.apiKey}`);
46
+ }
47
+ if (config.originHeader)
48
+ headers.set("x-origin-header", config.originHeader);
49
+ return instrumented(url, { ...rest, headers, operation });
50
+ }
51
+ return {
52
+ request,
53
+ /** A REST call (`/rest/<store>/V1/...`) returning its parsed JSON body. */
54
+ async rest(path, init) {
55
+ const headers = new Headers(init.headers);
56
+ if (init.body !== undefined && !headers.has("content-type")) {
57
+ headers.set("content-type", "application/json");
58
+ }
59
+ const response = await request(path, { ...init, headers });
60
+ if (!response.ok)
61
+ throw new MagentoError(init.operation, response.status);
62
+ if (response.status === 204)
63
+ return undefined;
64
+ return (await response.json());
65
+ },
66
+ /**
67
+ * A GraphQL operation on `/graphql`. `operationName` is sent with the
68
+ * request and is the operation label. `headers` adds per-call headers,
69
+ * such as `Store` or a customer `Authorization` token. Storefront
70
+ * GraphQL is public, so the `apiKey` is sent only with
71
+ * `authenticated: true`, and never over a customer token.
72
+ */
73
+ async graphql(query, variables, init) {
74
+ const { operationName, ...rest } = init;
75
+ const headers = new Headers(rest.headers);
76
+ headers.set("content-type", "application/json");
77
+ const response = await request("/graphql", {
78
+ operation: operationName,
79
+ authenticated: rest.authenticated ?? false,
80
+ method: "POST",
81
+ headers,
82
+ body: JSON.stringify({ query, variables, operationName }),
83
+ });
84
+ if (!response.ok)
85
+ throw new MagentoError(operationName, response.status);
86
+ const body = (await response.json());
87
+ if (body.errors?.length) {
88
+ throw new MagentoError(operationName, response.status, body.errors.length);
89
+ }
90
+ if (body.data === undefined)
91
+ throw new MagentoError(operationName, response.status);
92
+ return body.data;
93
+ },
94
+ };
95
+ }
96
+ //# sourceMappingURL=magentoClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"magentoClient.js","sourceRoot":"","sources":["../src/magentoClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,uBAAuB,E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C,CAAC;YAC1C,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,kBAAkB,CAAC,CAAC;YAChD,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,UAAU,EAAE;gBACzC,SAAS,EAAE,aAAa;gBACxB,aAAa,EAAE,IAAI,CAAC,aAAa,IAAI,KAAK;gBAC1C,MAAM,EAAE,MAAM;gBACd,OAAO;gBACP,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,CAAC;aAC1D,CAAC,CAAC;YACH,IAAI,CAAC,QAAQ,CAAC,EAAE;gBAAE,MAAM,IAAI,YAAY,CAAC,aAAa,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;YACzE,MAAM,IAAI,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAyC,CAAC;YAC7E,IAAI,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC;gBACxB,MAAM,IAAI,YAAY,CAAC,aAAa,EAAE,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YAC7E,CAAC;YACD,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS;gBAAE,MAAM,IAAI,YAAY,CAAC,aAAa,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;YACpF,OAAO,IAAI,CAAC,IAAI,CAAC;QACnB,CAAC;KACF,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/apps-magento",
3
- "version": "8.1.0-next.1",
3
+ "version": "8.1.0-next.2",
4
4
  "type": "module",
5
5
  "description": "Thin Magento (Adobe Commerce) API client for Deco sites",
6
6
  "repository": {
@@ -9,24 +9,28 @@
9
9
  "directory": "packages/apps-magento"
10
10
  },
11
11
  "files": [
12
+ "dist",
12
13
  "src",
13
14
  "!src/**/*.test.ts",
14
15
  "!src/**/__tests__"
15
16
  ],
16
- "main": "./src/index.ts",
17
+ "main": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
17
19
  "exports": {
18
- ".": "./src/index.ts"
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "source": "./src/index.ts",
23
+ "default": "./dist/index.js"
24
+ }
19
25
  },
20
26
  "scripts": {
21
- "build": "tsc",
27
+ "build": "node ../../scripts/tsc.mjs --clean dist -p tsconfig.build.json",
28
+ "prepack": "npm run build",
22
29
  "test": "vitest run --root ../.. packages/apps-magento/",
23
- "typecheck": "tsc --noEmit"
30
+ "typecheck": "node ../../scripts/tsc.mjs -p tsconfig.json"
24
31
  },
25
32
  "dependencies": {
26
- "@decocms/blocks": "8.1.0-next.1"
27
- },
28
- "devDependencies": {
29
- "typescript": "^5.9.0"
33
+ "@decocms/blocks": "8.1.0-next.2"
30
34
  },
31
35
  "publishConfig": {
32
36
  "registry": "https://registry.npmjs.org",
package/src/index.ts CHANGED
@@ -6,4 +6,4 @@ export {
6
6
  type MagentoClientOptions,
7
7
  MagentoError,
8
8
  type MagentoRequestInit,
9
- } from "./magentoClient";
9
+ } from "./magentoClient.ts";