@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/dist/index.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
import createClient from "openapi-fetch";
|
|
3
|
+
var SAFE_LANGUAGE_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD", "OPTIONS"]);
|
|
4
|
+
function buildDefaultHeaders(options) {
|
|
5
|
+
const headers = new Headers(options.headers);
|
|
6
|
+
headers.set("X-Vendor-ID", options.vendorId);
|
|
7
|
+
if (options.token) {
|
|
8
|
+
headers.set("Authorization", `Bearer ${options.token}`);
|
|
9
|
+
}
|
|
10
|
+
if (options.language) {
|
|
11
|
+
headers.set("Accept-Language", options.language);
|
|
12
|
+
}
|
|
13
|
+
return headers;
|
|
14
|
+
}
|
|
15
|
+
function createAllClient(options) {
|
|
16
|
+
const runtimeFetch = options.fetch ?? fetch;
|
|
17
|
+
const authFetch = (input, init) => {
|
|
18
|
+
const requestInput = applyLanguageQueryParam(input, init, options);
|
|
19
|
+
const headers = buildDefaultHeaders(options);
|
|
20
|
+
const requestHeaders = new Headers(
|
|
21
|
+
isRequestInstance(requestInput) ? requestInput.headers : void 0
|
|
22
|
+
);
|
|
23
|
+
const initHeaders = new Headers(init?.headers);
|
|
24
|
+
initHeaders.forEach((value, key) => {
|
|
25
|
+
requestHeaders.set(key, value);
|
|
26
|
+
});
|
|
27
|
+
requestHeaders.forEach((value, key) => {
|
|
28
|
+
headers.set(key, value);
|
|
29
|
+
});
|
|
30
|
+
return runtimeFetch(requestInput, {
|
|
31
|
+
...init,
|
|
32
|
+
headers
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
return createClient({
|
|
36
|
+
baseUrl: options.baseUrl,
|
|
37
|
+
fetch: authFetch
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function applyLanguageQueryParam(input, init, options) {
|
|
41
|
+
if (!options.language || options.appendLanguageQuery === false) {
|
|
42
|
+
return input;
|
|
43
|
+
}
|
|
44
|
+
const method = resolveMethod(input, init);
|
|
45
|
+
if (!SAFE_LANGUAGE_METHODS.has(method)) {
|
|
46
|
+
return input;
|
|
47
|
+
}
|
|
48
|
+
const resolvedUrl = resolveUrl(input, options.baseUrl);
|
|
49
|
+
if (resolvedUrl === null || resolvedUrl.searchParams.has("lang")) {
|
|
50
|
+
return input;
|
|
51
|
+
}
|
|
52
|
+
resolvedUrl.searchParams.set("lang", options.language);
|
|
53
|
+
if (isRequestInstance(input)) {
|
|
54
|
+
return new Request(resolvedUrl.toString(), input);
|
|
55
|
+
}
|
|
56
|
+
return resolvedUrl.toString();
|
|
57
|
+
}
|
|
58
|
+
function resolveMethod(input, init) {
|
|
59
|
+
if (init?.method) {
|
|
60
|
+
return init.method.toUpperCase();
|
|
61
|
+
}
|
|
62
|
+
if (isRequestInstance(input)) {
|
|
63
|
+
return input.method.toUpperCase();
|
|
64
|
+
}
|
|
65
|
+
return "GET";
|
|
66
|
+
}
|
|
67
|
+
function resolveUrl(input, baseUrl) {
|
|
68
|
+
try {
|
|
69
|
+
if (typeof input === "string") {
|
|
70
|
+
return new URL(input, baseUrl);
|
|
71
|
+
}
|
|
72
|
+
if (input instanceof URL) {
|
|
73
|
+
return new URL(input.toString(), baseUrl);
|
|
74
|
+
}
|
|
75
|
+
if (isRequestInstance(input)) {
|
|
76
|
+
return new URL(input.url, baseUrl);
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
} catch {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function isRequestInstance(input) {
|
|
84
|
+
return typeof Request !== "undefined" && input instanceof Request;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// src/generated/retryability.ts
|
|
88
|
+
var RETRYABLE_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
89
|
+
"KEYCLOAK_ERROR",
|
|
90
|
+
"MERCHANT_AT_CAPACITY",
|
|
91
|
+
"OTP_RATE_LIMIT_EXCEEDED",
|
|
92
|
+
"RATE_LIMITED",
|
|
93
|
+
"REFUND_PROCESSING_FAILED",
|
|
94
|
+
"SUBSCRIPTION_STRIPE_ERROR"
|
|
95
|
+
]);
|
|
96
|
+
var RETRYABLE_HTTP_STATUSES = /* @__PURE__ */ new Set([429, 502]);
|
|
97
|
+
|
|
98
|
+
// src/errors.ts
|
|
99
|
+
var SDKError = class extends Error {
|
|
100
|
+
status;
|
|
101
|
+
code;
|
|
102
|
+
requestId;
|
|
103
|
+
retryable;
|
|
104
|
+
body;
|
|
105
|
+
constructor(message, status, options) {
|
|
106
|
+
super(message);
|
|
107
|
+
this.name = "SDKError";
|
|
108
|
+
this.status = status;
|
|
109
|
+
this.code = options?.code;
|
|
110
|
+
this.requestId = options?.requestId;
|
|
111
|
+
this.retryable = options?.retryable ?? false;
|
|
112
|
+
this.body = options?.body;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
async function sdkErrorFromResponse(response) {
|
|
116
|
+
let body;
|
|
117
|
+
try {
|
|
118
|
+
body = await response.clone().json();
|
|
119
|
+
} catch {
|
|
120
|
+
body = await response.clone().text();
|
|
121
|
+
}
|
|
122
|
+
const payload = toObject(body);
|
|
123
|
+
const code = readString(payload, "error", "code");
|
|
124
|
+
const requestId = readString(payload, "request_id");
|
|
125
|
+
const retryable = isRetryable(response.status, code);
|
|
126
|
+
const message = code ? `Storefront API request failed with ${response.status} (${code})` : `Storefront API request failed with ${response.status}`;
|
|
127
|
+
return new SDKError(message, response.status, {
|
|
128
|
+
code,
|
|
129
|
+
requestId,
|
|
130
|
+
retryable,
|
|
131
|
+
body
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
function isRetryable(status, code) {
|
|
135
|
+
if (code && RETRYABLE_ERROR_CODES.has(code)) {
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
return RETRYABLE_HTTP_STATUSES.has(status);
|
|
139
|
+
}
|
|
140
|
+
function toObject(value) {
|
|
141
|
+
return value !== null && typeof value === "object" ? value : null;
|
|
142
|
+
}
|
|
143
|
+
function readString(value, ...keys) {
|
|
144
|
+
let current = value;
|
|
145
|
+
for (const key of keys) {
|
|
146
|
+
if (current === null || typeof current !== "object") {
|
|
147
|
+
return void 0;
|
|
148
|
+
}
|
|
149
|
+
current = current[key];
|
|
150
|
+
}
|
|
151
|
+
return typeof current === "string" ? current : void 0;
|
|
152
|
+
}
|
|
153
|
+
export {
|
|
154
|
+
SDKError,
|
|
155
|
+
buildDefaultHeaders,
|
|
156
|
+
createAllClient,
|
|
157
|
+
sdkErrorFromResponse
|
|
158
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@poweredbysous/storefront-all-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Type-safe TypeScript SDK for all SOUS Storefront Backend endpoints",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"private": false,
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "dist/index.cjs",
|
|
12
|
+
"module": "dist/index.js",
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js",
|
|
18
|
+
"require": "./dist/index.cjs"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=20",
|
|
26
|
+
"npm": ">=10"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"generate": "uv run python ../../scripts/generate_sdk_retryability.py --schema ../../openapi/openapi.v1.json --output src/generated/retryability.ts && openapi-typescript ../../openapi/openapi.v1.json -o src/generated/schema.ts",
|
|
30
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --clean",
|
|
31
|
+
"test": "vitest run",
|
|
32
|
+
"typecheck": "tsc --noEmit",
|
|
33
|
+
"lint": "tsc --noEmit",
|
|
34
|
+
"prepack": "npm run generate && npm run build"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"openapi-fetch": "^0.14.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "^22.13.5",
|
|
41
|
+
"openapi-typescript": "^7.6.1",
|
|
42
|
+
"tsup": "^8.3.5",
|
|
43
|
+
"typescript": "^5.8.2",
|
|
44
|
+
"vitest": "^3.2.4"
|
|
45
|
+
}
|
|
46
|
+
}
|