@poweredbysous/storefront-all-sdk 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.
- package/README.md +55 -0
- package/dist/index.cjs +198 -0
- package/dist/index.d.cts +21298 -0
- package/dist/index.d.ts +21298 -0
- package/dist/index.js +158 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# @poweredbysous/storefront-all-sdk
|
|
2
|
+
|
|
3
|
+
Type-safe TypeScript SDK for all Storefront Backend endpoints (`/api/v1`) generated from OpenAPI.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @poweredbysous/storefront-all-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pnpm add @poweredbysous/storefront-all-sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
yarn add @poweredbysous/storefront-all-sdk
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { createAllClient } from "@poweredbysous/storefront-all-sdk";
|
|
23
|
+
|
|
24
|
+
const client = createAllClient({
|
|
25
|
+
baseUrl: "https://api.example.com",
|
|
26
|
+
vendorId: "BRASSERIE_VD_01",
|
|
27
|
+
language: "nl",
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const response = await client.GET("/api/v1/products/");
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`language` sets the `Accept-Language` request header and, by default, appends
|
|
34
|
+
`?lang=<language>` to `GET`/`HEAD`/`OPTIONS` requests when `lang` is missing.
|
|
35
|
+
Disable query injection with `appendLanguageQuery: false`.
|
|
36
|
+
|
|
37
|
+
## Error Handling
|
|
38
|
+
|
|
39
|
+
`sdkErrorFromResponse(response)` returns `SDKError` with:
|
|
40
|
+
|
|
41
|
+
- `status` HTTP status code
|
|
42
|
+
- `code` machine-readable API error code (when present)
|
|
43
|
+
- `retryable` retry hint based on status/code
|
|
44
|
+
- `requestId` correlation ID from `request_id` (when present)
|
|
45
|
+
|
|
46
|
+
## Development
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
npm ci
|
|
50
|
+
npm run generate
|
|
51
|
+
npm run test
|
|
52
|
+
npm run build
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Generated types live in `src/generated/schema.ts`.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
SDKError: () => SDKError,
|
|
34
|
+
buildDefaultHeaders: () => buildDefaultHeaders,
|
|
35
|
+
createAllClient: () => createAllClient,
|
|
36
|
+
sdkErrorFromResponse: () => sdkErrorFromResponse
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
|
+
|
|
40
|
+
// src/client.ts
|
|
41
|
+
var import_openapi_fetch = __toESM(require("openapi-fetch"), 1);
|
|
42
|
+
var SAFE_LANGUAGE_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD", "OPTIONS"]);
|
|
43
|
+
function buildDefaultHeaders(options) {
|
|
44
|
+
const headers = new Headers(options.headers);
|
|
45
|
+
headers.set("X-Vendor-ID", options.vendorId);
|
|
46
|
+
if (options.token) {
|
|
47
|
+
headers.set("Authorization", `Bearer ${options.token}`);
|
|
48
|
+
}
|
|
49
|
+
if (options.language) {
|
|
50
|
+
headers.set("Accept-Language", options.language);
|
|
51
|
+
}
|
|
52
|
+
return headers;
|
|
53
|
+
}
|
|
54
|
+
function createAllClient(options) {
|
|
55
|
+
const runtimeFetch = options.fetch ?? fetch;
|
|
56
|
+
const authFetch = (input, init) => {
|
|
57
|
+
const requestInput = applyLanguageQueryParam(input, init, options);
|
|
58
|
+
const headers = buildDefaultHeaders(options);
|
|
59
|
+
const requestHeaders = new Headers(
|
|
60
|
+
isRequestInstance(requestInput) ? requestInput.headers : void 0
|
|
61
|
+
);
|
|
62
|
+
const initHeaders = new Headers(init?.headers);
|
|
63
|
+
initHeaders.forEach((value, key) => {
|
|
64
|
+
requestHeaders.set(key, value);
|
|
65
|
+
});
|
|
66
|
+
requestHeaders.forEach((value, key) => {
|
|
67
|
+
headers.set(key, value);
|
|
68
|
+
});
|
|
69
|
+
return runtimeFetch(requestInput, {
|
|
70
|
+
...init,
|
|
71
|
+
headers
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
return (0, import_openapi_fetch.default)({
|
|
75
|
+
baseUrl: options.baseUrl,
|
|
76
|
+
fetch: authFetch
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
function applyLanguageQueryParam(input, init, options) {
|
|
80
|
+
if (!options.language || options.appendLanguageQuery === false) {
|
|
81
|
+
return input;
|
|
82
|
+
}
|
|
83
|
+
const method = resolveMethod(input, init);
|
|
84
|
+
if (!SAFE_LANGUAGE_METHODS.has(method)) {
|
|
85
|
+
return input;
|
|
86
|
+
}
|
|
87
|
+
const resolvedUrl = resolveUrl(input, options.baseUrl);
|
|
88
|
+
if (resolvedUrl === null || resolvedUrl.searchParams.has("lang")) {
|
|
89
|
+
return input;
|
|
90
|
+
}
|
|
91
|
+
resolvedUrl.searchParams.set("lang", options.language);
|
|
92
|
+
if (isRequestInstance(input)) {
|
|
93
|
+
return new Request(resolvedUrl.toString(), input);
|
|
94
|
+
}
|
|
95
|
+
return resolvedUrl.toString();
|
|
96
|
+
}
|
|
97
|
+
function resolveMethod(input, init) {
|
|
98
|
+
if (init?.method) {
|
|
99
|
+
return init.method.toUpperCase();
|
|
100
|
+
}
|
|
101
|
+
if (isRequestInstance(input)) {
|
|
102
|
+
return input.method.toUpperCase();
|
|
103
|
+
}
|
|
104
|
+
return "GET";
|
|
105
|
+
}
|
|
106
|
+
function resolveUrl(input, baseUrl) {
|
|
107
|
+
try {
|
|
108
|
+
if (typeof input === "string") {
|
|
109
|
+
return new URL(input, baseUrl);
|
|
110
|
+
}
|
|
111
|
+
if (input instanceof URL) {
|
|
112
|
+
return new URL(input.toString(), baseUrl);
|
|
113
|
+
}
|
|
114
|
+
if (isRequestInstance(input)) {
|
|
115
|
+
return new URL(input.url, baseUrl);
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
} catch {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function isRequestInstance(input) {
|
|
123
|
+
return typeof Request !== "undefined" && input instanceof Request;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// src/generated/retryability.ts
|
|
127
|
+
var RETRYABLE_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
128
|
+
"KEYCLOAK_ERROR",
|
|
129
|
+
"MERCHANT_AT_CAPACITY",
|
|
130
|
+
"OTP_RATE_LIMIT_EXCEEDED",
|
|
131
|
+
"RATE_LIMITED",
|
|
132
|
+
"REFUND_PROCESSING_FAILED",
|
|
133
|
+
"SUBSCRIPTION_STRIPE_ERROR"
|
|
134
|
+
]);
|
|
135
|
+
var RETRYABLE_HTTP_STATUSES = /* @__PURE__ */ new Set([429, 502]);
|
|
136
|
+
|
|
137
|
+
// src/errors.ts
|
|
138
|
+
var SDKError = class extends Error {
|
|
139
|
+
status;
|
|
140
|
+
code;
|
|
141
|
+
requestId;
|
|
142
|
+
retryable;
|
|
143
|
+
body;
|
|
144
|
+
constructor(message, status, options) {
|
|
145
|
+
super(message);
|
|
146
|
+
this.name = "SDKError";
|
|
147
|
+
this.status = status;
|
|
148
|
+
this.code = options?.code;
|
|
149
|
+
this.requestId = options?.requestId;
|
|
150
|
+
this.retryable = options?.retryable ?? false;
|
|
151
|
+
this.body = options?.body;
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
async function sdkErrorFromResponse(response) {
|
|
155
|
+
let body;
|
|
156
|
+
try {
|
|
157
|
+
body = await response.clone().json();
|
|
158
|
+
} catch {
|
|
159
|
+
body = await response.clone().text();
|
|
160
|
+
}
|
|
161
|
+
const payload = toObject(body);
|
|
162
|
+
const code = readString(payload, "error", "code");
|
|
163
|
+
const requestId = readString(payload, "request_id");
|
|
164
|
+
const retryable = isRetryable(response.status, code);
|
|
165
|
+
const message = code ? `Storefront API request failed with ${response.status} (${code})` : `Storefront API request failed with ${response.status}`;
|
|
166
|
+
return new SDKError(message, response.status, {
|
|
167
|
+
code,
|
|
168
|
+
requestId,
|
|
169
|
+
retryable,
|
|
170
|
+
body
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
function isRetryable(status, code) {
|
|
174
|
+
if (code && RETRYABLE_ERROR_CODES.has(code)) {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
return RETRYABLE_HTTP_STATUSES.has(status);
|
|
178
|
+
}
|
|
179
|
+
function toObject(value) {
|
|
180
|
+
return value !== null && typeof value === "object" ? value : null;
|
|
181
|
+
}
|
|
182
|
+
function readString(value, ...keys) {
|
|
183
|
+
let current = value;
|
|
184
|
+
for (const key of keys) {
|
|
185
|
+
if (current === null || typeof current !== "object") {
|
|
186
|
+
return void 0;
|
|
187
|
+
}
|
|
188
|
+
current = current[key];
|
|
189
|
+
}
|
|
190
|
+
return typeof current === "string" ? current : void 0;
|
|
191
|
+
}
|
|
192
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
193
|
+
0 && (module.exports = {
|
|
194
|
+
SDKError,
|
|
195
|
+
buildDefaultHeaders,
|
|
196
|
+
createAllClient,
|
|
197
|
+
sdkErrorFromResponse
|
|
198
|
+
});
|