@devizovaburza/payments-api-sdk 1.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 +88 -0
- package/dist/v1/index.cjs +376 -0
- package/dist/v1/index.d.cts +3394 -0
- package/dist/v1/index.d.mts +3394 -0
- package/dist/v1/index.d.ts +3394 -0
- package/dist/v1/index.mjs +372 -0
- package/package.json +25 -0
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
// src/utils/cookie.ts
|
|
2
|
+
var _serialize = (name, value, opt = {}) => {
|
|
3
|
+
let cookie = `${name}=${value}`;
|
|
4
|
+
if (name.startsWith("__Secure-") && !opt.secure) {
|
|
5
|
+
throw new Error("__Secure- Cookie must have Secure attributes");
|
|
6
|
+
}
|
|
7
|
+
if (name.startsWith("__Host-")) {
|
|
8
|
+
if (!opt.secure) {
|
|
9
|
+
throw new Error("__Host- Cookie must have Secure attributes");
|
|
10
|
+
}
|
|
11
|
+
if (opt.path !== "/") {
|
|
12
|
+
throw new Error('__Host- Cookie must have Path attributes with "/"');
|
|
13
|
+
}
|
|
14
|
+
if (opt.domain) {
|
|
15
|
+
throw new Error("__Host- Cookie must not have Domain attributes");
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if (opt && typeof opt.maxAge === "number" && opt.maxAge >= 0) {
|
|
19
|
+
if (opt.maxAge > 3456e4) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
"Cookies Max-Age SHOULD NOT be greater than 400 days (34560000 seconds) in duration."
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
cookie += `; Max-Age=${opt.maxAge | 0}`;
|
|
25
|
+
}
|
|
26
|
+
if (opt.domain && opt.prefix !== "host") {
|
|
27
|
+
cookie += `; Domain=${opt.domain}`;
|
|
28
|
+
}
|
|
29
|
+
if (opt.path) {
|
|
30
|
+
cookie += `; Path=${opt.path}`;
|
|
31
|
+
}
|
|
32
|
+
if (opt.expires) {
|
|
33
|
+
if (opt.expires.getTime() - Date.now() > 3456e7) {
|
|
34
|
+
throw new Error(
|
|
35
|
+
"Cookies Expires SHOULD NOT be greater than 400 days (34560000 seconds) in the future."
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
cookie += `; Expires=${opt.expires.toUTCString()}`;
|
|
39
|
+
}
|
|
40
|
+
if (opt.httpOnly) {
|
|
41
|
+
cookie += "; HttpOnly";
|
|
42
|
+
}
|
|
43
|
+
if (opt.secure) {
|
|
44
|
+
cookie += "; Secure";
|
|
45
|
+
}
|
|
46
|
+
if (opt.sameSite) {
|
|
47
|
+
cookie += `; SameSite=${opt.sameSite.charAt(0).toUpperCase() + opt.sameSite.slice(1)}`;
|
|
48
|
+
}
|
|
49
|
+
if (opt.priority) {
|
|
50
|
+
cookie += `; Priority=${opt.priority.charAt(0).toUpperCase() + opt.priority.slice(1)}`;
|
|
51
|
+
}
|
|
52
|
+
if (opt.partitioned) {
|
|
53
|
+
if (!opt.secure) {
|
|
54
|
+
throw new Error("Partitioned Cookie must have Secure attributes");
|
|
55
|
+
}
|
|
56
|
+
cookie += "; Partitioned";
|
|
57
|
+
}
|
|
58
|
+
return cookie;
|
|
59
|
+
};
|
|
60
|
+
var serialize = (name, value, opt) => {
|
|
61
|
+
value = encodeURIComponent(value);
|
|
62
|
+
return _serialize(name, value, opt);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// src/client/utils.ts
|
|
66
|
+
var mergePath = (base, path) => {
|
|
67
|
+
base = base.replace(/\/+$/, "");
|
|
68
|
+
base = base + "/";
|
|
69
|
+
path = path.replace(/^\/+/, "");
|
|
70
|
+
return base + path;
|
|
71
|
+
};
|
|
72
|
+
var replaceUrlParam = (urlString, params) => {
|
|
73
|
+
for (const [k, v] of Object.entries(params)) {
|
|
74
|
+
const reg = new RegExp("/:" + k + "(?:{[^/]+})?\\??");
|
|
75
|
+
urlString = urlString.replace(reg, v ? `/${v}` : "");
|
|
76
|
+
}
|
|
77
|
+
return urlString;
|
|
78
|
+
};
|
|
79
|
+
var buildSearchParams = (query) => {
|
|
80
|
+
const searchParams = new URLSearchParams();
|
|
81
|
+
for (const [k, v] of Object.entries(query)) {
|
|
82
|
+
if (v === void 0) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (Array.isArray(v)) {
|
|
86
|
+
for (const v2 of v) {
|
|
87
|
+
searchParams.append(k, v2);
|
|
88
|
+
}
|
|
89
|
+
} else {
|
|
90
|
+
searchParams.set(k, v);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return searchParams;
|
|
94
|
+
};
|
|
95
|
+
var replaceUrlProtocol = (urlString, protocol) => {
|
|
96
|
+
switch (protocol) {
|
|
97
|
+
case "ws":
|
|
98
|
+
return urlString.replace(/^http/, "ws");
|
|
99
|
+
case "http":
|
|
100
|
+
return urlString.replace(/^ws/, "http");
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
var removeIndexString = (urlString) => {
|
|
104
|
+
if (/^https?:\/\/[^\/]+?\/index(?=\?|$)/.test(urlString)) {
|
|
105
|
+
return urlString.replace(/\/index(?=\?|$)/, "/");
|
|
106
|
+
}
|
|
107
|
+
return urlString.replace(/\/index(?=\?|$)/, "");
|
|
108
|
+
};
|
|
109
|
+
function isObject(item) {
|
|
110
|
+
return typeof item === "object" && item !== null && !Array.isArray(item);
|
|
111
|
+
}
|
|
112
|
+
function deepMerge(target, source) {
|
|
113
|
+
if (!isObject(target) && !isObject(source)) {
|
|
114
|
+
return source;
|
|
115
|
+
}
|
|
116
|
+
const merged = { ...target };
|
|
117
|
+
for (const key in source) {
|
|
118
|
+
const value = source[key];
|
|
119
|
+
if (isObject(merged[key]) && isObject(value)) {
|
|
120
|
+
merged[key] = deepMerge(merged[key], value);
|
|
121
|
+
} else {
|
|
122
|
+
merged[key] = value;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return merged;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// src/client/client.ts
|
|
129
|
+
var createProxy = (callback, path) => {
|
|
130
|
+
const proxy = new Proxy(() => {
|
|
131
|
+
}, {
|
|
132
|
+
get(_obj, key) {
|
|
133
|
+
if (typeof key !== "string" || key === "then") {
|
|
134
|
+
return void 0;
|
|
135
|
+
}
|
|
136
|
+
return createProxy(callback, [...path, key]);
|
|
137
|
+
},
|
|
138
|
+
apply(_1, _2, args) {
|
|
139
|
+
return callback({
|
|
140
|
+
path,
|
|
141
|
+
args
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
return proxy;
|
|
146
|
+
};
|
|
147
|
+
var ClientRequestImpl = class {
|
|
148
|
+
url;
|
|
149
|
+
method;
|
|
150
|
+
buildSearchParams;
|
|
151
|
+
queryParams = void 0;
|
|
152
|
+
pathParams = {};
|
|
153
|
+
rBody;
|
|
154
|
+
cType = void 0;
|
|
155
|
+
constructor(url, method, options) {
|
|
156
|
+
this.url = url;
|
|
157
|
+
this.method = method;
|
|
158
|
+
this.buildSearchParams = options.buildSearchParams;
|
|
159
|
+
}
|
|
160
|
+
fetch = async (args, opt) => {
|
|
161
|
+
if (args) {
|
|
162
|
+
if (args.query) {
|
|
163
|
+
this.queryParams = this.buildSearchParams(args.query);
|
|
164
|
+
}
|
|
165
|
+
if (args.form) {
|
|
166
|
+
const form = new FormData();
|
|
167
|
+
for (const [k, v] of Object.entries(args.form)) {
|
|
168
|
+
if (Array.isArray(v)) {
|
|
169
|
+
for (const v2 of v) {
|
|
170
|
+
form.append(k, v2);
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
form.append(k, v);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
this.rBody = form;
|
|
177
|
+
}
|
|
178
|
+
if (args.json) {
|
|
179
|
+
this.rBody = JSON.stringify(args.json);
|
|
180
|
+
this.cType = "application/json";
|
|
181
|
+
}
|
|
182
|
+
if (args.param) {
|
|
183
|
+
this.pathParams = args.param;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
let methodUpperCase = this.method.toUpperCase();
|
|
187
|
+
const headerValues = {
|
|
188
|
+
...args?.header,
|
|
189
|
+
...typeof opt?.headers === "function" ? await opt.headers() : opt?.headers
|
|
190
|
+
};
|
|
191
|
+
if (args?.cookie) {
|
|
192
|
+
const cookies = [];
|
|
193
|
+
for (const [key, value] of Object.entries(args.cookie)) {
|
|
194
|
+
cookies.push(serialize(key, value, { path: "/" }));
|
|
195
|
+
}
|
|
196
|
+
headerValues["Cookie"] = cookies.join(",");
|
|
197
|
+
}
|
|
198
|
+
if (this.cType) {
|
|
199
|
+
headerValues["Content-Type"] = this.cType;
|
|
200
|
+
}
|
|
201
|
+
const headers = new Headers(headerValues ?? void 0);
|
|
202
|
+
let url = this.url;
|
|
203
|
+
url = removeIndexString(url);
|
|
204
|
+
url = replaceUrlParam(url, this.pathParams);
|
|
205
|
+
if (this.queryParams) {
|
|
206
|
+
url = url + "?" + this.queryParams.toString();
|
|
207
|
+
}
|
|
208
|
+
methodUpperCase = this.method.toUpperCase();
|
|
209
|
+
const setBody = !(methodUpperCase === "GET" || methodUpperCase === "HEAD");
|
|
210
|
+
return (opt?.fetch || fetch)(url, {
|
|
211
|
+
body: setBody ? this.rBody : void 0,
|
|
212
|
+
method: methodUpperCase,
|
|
213
|
+
headers,
|
|
214
|
+
...opt?.init
|
|
215
|
+
});
|
|
216
|
+
};
|
|
217
|
+
};
|
|
218
|
+
var hc = (baseUrl, options) => createProxy(function proxyCallback(opts) {
|
|
219
|
+
const buildSearchParamsOption = options?.buildSearchParams ?? buildSearchParams;
|
|
220
|
+
const parts = [...opts.path];
|
|
221
|
+
const lastParts = parts.slice(-3).reverse();
|
|
222
|
+
if (lastParts[0] === "toString") {
|
|
223
|
+
if (lastParts[1] === "name") {
|
|
224
|
+
return lastParts[2] || "";
|
|
225
|
+
}
|
|
226
|
+
return proxyCallback.toString();
|
|
227
|
+
}
|
|
228
|
+
if (lastParts[0] === "valueOf") {
|
|
229
|
+
if (lastParts[1] === "name") {
|
|
230
|
+
return lastParts[2] || "";
|
|
231
|
+
}
|
|
232
|
+
return proxyCallback;
|
|
233
|
+
}
|
|
234
|
+
let method = "";
|
|
235
|
+
if (/^\$/.test(lastParts[0])) {
|
|
236
|
+
const last = parts.pop();
|
|
237
|
+
if (last) {
|
|
238
|
+
method = last.replace(/^\$/, "");
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
const path = parts.join("/");
|
|
242
|
+
const url = mergePath(baseUrl, path);
|
|
243
|
+
if (method === "url") {
|
|
244
|
+
let result = url;
|
|
245
|
+
if (opts.args[0]) {
|
|
246
|
+
if (opts.args[0].param) {
|
|
247
|
+
result = replaceUrlParam(url, opts.args[0].param);
|
|
248
|
+
}
|
|
249
|
+
if (opts.args[0].query) {
|
|
250
|
+
result = result + "?" + buildSearchParamsOption(opts.args[0].query).toString();
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
result = removeIndexString(result);
|
|
254
|
+
return new URL(result);
|
|
255
|
+
}
|
|
256
|
+
if (method === "ws") {
|
|
257
|
+
const webSocketUrl = replaceUrlProtocol(
|
|
258
|
+
opts.args[0] && opts.args[0].param ? replaceUrlParam(url, opts.args[0].param) : url,
|
|
259
|
+
"ws"
|
|
260
|
+
);
|
|
261
|
+
const targetUrl = new URL(webSocketUrl);
|
|
262
|
+
const queryParams = opts.args[0]?.query;
|
|
263
|
+
if (queryParams) {
|
|
264
|
+
Object.entries(queryParams).forEach(([key, value]) => {
|
|
265
|
+
if (Array.isArray(value)) {
|
|
266
|
+
value.forEach((item) => targetUrl.searchParams.append(key, item));
|
|
267
|
+
} else {
|
|
268
|
+
targetUrl.searchParams.set(key, value);
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
const establishWebSocket = (...args) => {
|
|
273
|
+
if (options?.webSocket !== void 0 && typeof options.webSocket === "function") {
|
|
274
|
+
return options.webSocket(...args);
|
|
275
|
+
}
|
|
276
|
+
return new WebSocket(...args);
|
|
277
|
+
};
|
|
278
|
+
return establishWebSocket(targetUrl.toString());
|
|
279
|
+
}
|
|
280
|
+
const req = new ClientRequestImpl(url, method, {
|
|
281
|
+
buildSearchParams: buildSearchParamsOption
|
|
282
|
+
});
|
|
283
|
+
if (method) {
|
|
284
|
+
options ??= {};
|
|
285
|
+
const args = deepMerge(options, { ...opts.args[1] });
|
|
286
|
+
return req.fetch(opts.args[0], args);
|
|
287
|
+
}
|
|
288
|
+
return req;
|
|
289
|
+
}, []);
|
|
290
|
+
|
|
291
|
+
const createPartnerApiClient = (...args) => {
|
|
292
|
+
return hc(...args);
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
const signPayload = async ({
|
|
296
|
+
payload,
|
|
297
|
+
privateKey
|
|
298
|
+
}) => {
|
|
299
|
+
const binaryPrivateKey = Uint8Array.from(
|
|
300
|
+
atob(privateKey),
|
|
301
|
+
(c) => c.charCodeAt(0)
|
|
302
|
+
);
|
|
303
|
+
const importedPrivateKey = await crypto.subtle.importKey(
|
|
304
|
+
"pkcs8",
|
|
305
|
+
binaryPrivateKey,
|
|
306
|
+
{
|
|
307
|
+
name: "RSASSA-PKCS1-v1_5",
|
|
308
|
+
hash: "SHA-256"
|
|
309
|
+
},
|
|
310
|
+
false,
|
|
311
|
+
["sign"]
|
|
312
|
+
);
|
|
313
|
+
const encodedPayload = new TextEncoder().encode(payload);
|
|
314
|
+
const signature = await crypto.subtle.sign(
|
|
315
|
+
{
|
|
316
|
+
name: "RSASSA-PKCS1-v1_5"
|
|
317
|
+
},
|
|
318
|
+
importedPrivateKey,
|
|
319
|
+
encodedPayload
|
|
320
|
+
);
|
|
321
|
+
const base64Signature = btoa(
|
|
322
|
+
String.fromCharCode(...new Uint8Array(signature))
|
|
323
|
+
);
|
|
324
|
+
return base64Signature;
|
|
325
|
+
};
|
|
326
|
+
const algParams = {
|
|
327
|
+
RSA: {
|
|
328
|
+
name: "RSASSA-PKCS1-v1_5",
|
|
329
|
+
hash: { name: "SHA-256" }
|
|
330
|
+
},
|
|
331
|
+
EC: {
|
|
332
|
+
name: "ECDSA",
|
|
333
|
+
namedCurve: "P-256"
|
|
334
|
+
},
|
|
335
|
+
HMAC: {
|
|
336
|
+
name: "HMAC",
|
|
337
|
+
hash: { name: "SHA-256" }
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
const verifyPayloadSignature = async ({
|
|
341
|
+
signature,
|
|
342
|
+
data,
|
|
343
|
+
publicKey,
|
|
344
|
+
algorithm = "RSA"
|
|
345
|
+
}) => {
|
|
346
|
+
const binaryPublicKey = Uint8Array.from(
|
|
347
|
+
atob(publicKey),
|
|
348
|
+
(c) => c.charCodeAt(0)
|
|
349
|
+
);
|
|
350
|
+
const format = algorithm === "HMAC" ? "raw" : "spki";
|
|
351
|
+
const importedPublicKey = await crypto.subtle.importKey(
|
|
352
|
+
format,
|
|
353
|
+
binaryPublicKey,
|
|
354
|
+
algParams[algorithm],
|
|
355
|
+
false,
|
|
356
|
+
["verify"]
|
|
357
|
+
);
|
|
358
|
+
const encodedPayload = new TextEncoder().encode(data);
|
|
359
|
+
const decodedSignature = Uint8Array.from(
|
|
360
|
+
atob(signature),
|
|
361
|
+
(c) => c.charCodeAt(0)
|
|
362
|
+
);
|
|
363
|
+
const isValid = await crypto.subtle.verify(
|
|
364
|
+
algParams[algorithm],
|
|
365
|
+
importedPublicKey,
|
|
366
|
+
decodedSignature,
|
|
367
|
+
encodedPayload
|
|
368
|
+
);
|
|
369
|
+
return isValid;
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
export { createPartnerApiClient, signPayload, verifyPayloadSignature };
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@devizovaburza/payments-api-sdk",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"author": "Devizová burza a.s.",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "unbuild",
|
|
9
|
+
"release": "nx release --projects=@devizovaburza/payments-api-sdk --skip-publish && git push --follow-tags"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
"./v1": {
|
|
13
|
+
"types": "./dist/v1/index.d.ts",
|
|
14
|
+
"import": "./dist/v1/index.mjs",
|
|
15
|
+
"require": "./dist/v1/index.cjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@develit-io/backend-sdk": "^9.10.0",
|
|
23
|
+
"unbuild": "^3.6.1"
|
|
24
|
+
}
|
|
25
|
+
}
|