@msm-platform/customer-sdk 0.0.0-stage → 0.1.0

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 @@
1
+ export {};
@@ -0,0 +1,29 @@
1
+ import { type Client } from "openapi-fetch";
2
+ import type { paths } from "./generated/api.js";
3
+ export { CustomerApiError, type ErrorCode, type HttpMethod, isCustomerApiError, type MethodOf, type ValidationIssue, } from "./errors.js";
4
+ export type { components, operations, paths } from "./generated/api.js";
5
+ export interface CustomerClientOptions {
6
+ /** The engine, or in proxy mode the proxy that forwards to it. */
7
+ baseUrl: string;
8
+ fetch?: typeof globalThis.fetch;
9
+ /**
10
+ * Server mode: the application client's ID and secret. The SDK sends them
11
+ * as HTTP Basic on every request, so there is no token to renew. Omit both
12
+ * in proxy mode, where the proxy adds the credentials on the server side.
13
+ */
14
+ clientId?: string;
15
+ clientSecret?: string;
16
+ }
17
+ export type CustomerClient = Client<paths>;
18
+ /**
19
+ * A typed client for MSM's customer API. Every non-2xx response throws
20
+ * `CustomerApiError`; nothing is retried.
21
+ */
22
+ export declare function createCustomerClient(options: CustomerClientOptions): CustomerClient;
23
+ /**
24
+ * The headers that send a customer's session with one call. The host stores
25
+ * the session token, for example in an HttpOnly cookie, and passes it here.
26
+ */
27
+ export declare function sessionHeaders(sessionToken: string): {
28
+ "X-Session-Token": string;
29
+ };
package/dist/index.js ADDED
@@ -0,0 +1,45 @@
1
+ import createClient from "openapi-fetch";
2
+ import { CustomerApiError } from "./errors.js";
3
+ export { CustomerApiError, isCustomerApiError, } from "./errors.js";
4
+ /**
5
+ * A typed client for MSM's customer API. Every non-2xx response throws
6
+ * `CustomerApiError`; nothing is retried.
7
+ */
8
+ export function createCustomerClient(options) {
9
+ const { clientId, clientSecret } = options;
10
+ if (clientId !== undefined || clientSecret !== undefined) {
11
+ // A secret in browser code is public: refuse before any request.
12
+ if (typeof window !== "undefined")
13
+ throw new Error("@msm-platform/customer-sdk: a client secret must never reach a browser. Run the SDK with clientId and clientSecret on your server only, or without them behind a proxy that adds them.");
14
+ if (!clientId || !clientSecret)
15
+ throw new Error("@msm-platform/customer-sdk: server mode needs both clientId and clientSecret.");
16
+ }
17
+ // HTTP Basic (RFC 7617) of the client's own ID and secret.
18
+ const authorization = clientSecret
19
+ ? `Basic ${btoa(String.fromCharCode(...new TextEncoder().encode(`${clientId}:${clientSecret}`)))}`
20
+ : undefined;
21
+ const client = createClient({
22
+ baseUrl: options.baseUrl,
23
+ fetch: options.fetch,
24
+ });
25
+ client.use({
26
+ async onRequest({ request }) {
27
+ if (authorization)
28
+ request.headers.set("Authorization", authorization);
29
+ return request;
30
+ },
31
+ async onResponse({ request, response, schemaPath }) {
32
+ if (response.ok)
33
+ return response;
34
+ throw await CustomerApiError.from(response, request.method, schemaPath);
35
+ },
36
+ });
37
+ return client;
38
+ }
39
+ /**
40
+ * The headers that send a customer's session with one call. The host stores
41
+ * the session token, for example in an HttpOnly cookie, and passes it here.
42
+ */
43
+ export function sessionHeaders(sessionToken) {
44
+ return { "X-Session-Token": sessionToken };
45
+ }
package/package.json CHANGED
@@ -1,6 +1,38 @@
1
1
  {
2
2
  "name": "@msm-platform/customer-sdk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Typed server-side client for MSM's customer API (/web)",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/Sveittur/msm-platform.git",
9
+ "directory": "packages/customer-sdk"
10
+ },
11
+ "type": "module",
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "import": "./dist/index.js"
19
+ }
20
+ },
21
+ "engines": {
22
+ "node": ">=20"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "dependencies": {
28
+ "openapi-fetch": "0.17.0"
29
+ },
30
+ "devDependencies": {
31
+ "typescript": "5.9.3"
32
+ },
33
+ "scripts": {
34
+ "build": "tsc -p tsconfig.build.json",
35
+ "typecheck": "tsc -p tsconfig.json --noEmit",
36
+ "test": "vitest run"
37
+ }
6
38
  }