@mainmoneyapp/js-http 0.0.0-stage → 0.1.5
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/LICENSE +21 -0
- package/README.md +3 -2
- package/dist/index.d.ts +43 -0
- package/dist/index.js +111 -0
- package/package.json +43 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MainMoney SARL
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mainmoneyapp/js-http
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
HTTP client for the merchant backend. Never send aggregator API keys.
|
|
4
|
+
See the [root README](../../README.md).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Session } from '@mainmoneyapp/js-core';
|
|
2
|
+
|
|
3
|
+
/** Injectable HTTP client for merchant-backend calls. */
|
|
4
|
+
type QueryValue = string | number | boolean | undefined;
|
|
5
|
+
type RequestOptions = {
|
|
6
|
+
headers?: Record<string, string>;
|
|
7
|
+
json?: Record<string, unknown>;
|
|
8
|
+
query?: Record<string, QueryValue>;
|
|
9
|
+
signal?: AbortSignal;
|
|
10
|
+
};
|
|
11
|
+
interface HttpClient {
|
|
12
|
+
request(method: string, uri: string, options?: RequestOptions): Promise<{
|
|
13
|
+
status: number;
|
|
14
|
+
bodyText: string;
|
|
15
|
+
}>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Default fetch client. */
|
|
19
|
+
|
|
20
|
+
declare class FetchHttpClient implements HttpClient {
|
|
21
|
+
private readonly fetchImpl;
|
|
22
|
+
private readonly timeoutMs;
|
|
23
|
+
constructor(fetchImpl: typeof fetch, timeoutMs?: number);
|
|
24
|
+
request(method: string, uri: string, options?: RequestOptions): Promise<{
|
|
25
|
+
status: number;
|
|
26
|
+
bodyText: string;
|
|
27
|
+
}>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** JSON client for merchantBackendUrl only. Never sends aggregator API keys. */
|
|
31
|
+
|
|
32
|
+
type MerchantHttp = {
|
|
33
|
+
get<T = unknown>(path: string, query?: Record<string, QueryValue>, extraHeaders?: Record<string, string>): Promise<T>;
|
|
34
|
+
post<T = unknown>(path: string, json?: Record<string, unknown>, extraHeaders?: Record<string, string>): Promise<T>;
|
|
35
|
+
};
|
|
36
|
+
type CreateHttpOptions = {
|
|
37
|
+
merchantBackendUrl: string;
|
|
38
|
+
http?: HttpClient;
|
|
39
|
+
};
|
|
40
|
+
declare function createHttp(session: Session, http?: HttpClient): MerchantHttp;
|
|
41
|
+
declare function decodeJson<T>(statusCode: number, bodyText: string): T;
|
|
42
|
+
|
|
43
|
+
export { type CreateHttpOptions, FetchHttpClient, type HttpClient, type MerchantHttp, type QueryValue, type RequestOptions, createHttp, decodeJson };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// src/fetch-client.ts
|
|
2
|
+
var FetchHttpClient = class {
|
|
3
|
+
constructor(fetchImpl, timeoutMs = 3e4) {
|
|
4
|
+
this.fetchImpl = fetchImpl;
|
|
5
|
+
this.timeoutMs = timeoutMs;
|
|
6
|
+
}
|
|
7
|
+
fetchImpl;
|
|
8
|
+
timeoutMs;
|
|
9
|
+
async request(method, uri, options = {}) {
|
|
10
|
+
const headers = { ...options.headers ?? {} };
|
|
11
|
+
let body;
|
|
12
|
+
if (options.json !== void 0) {
|
|
13
|
+
body = JSON.stringify(options.json);
|
|
14
|
+
headers.Accept ??= "application/json";
|
|
15
|
+
headers["Content-Type"] ??= "application/json";
|
|
16
|
+
}
|
|
17
|
+
const url = withQuery(uri, options.query);
|
|
18
|
+
const timeout = AbortSignal.timeout(this.timeoutMs);
|
|
19
|
+
const signal = options.signal === void 0 ? timeout : AbortSignal.any([timeout, options.signal]);
|
|
20
|
+
const response = await this.fetchImpl(url, { method, headers, body, signal });
|
|
21
|
+
return { status: response.status, bodyText: await response.text() };
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
function withQuery(uri, query) {
|
|
25
|
+
if (query === void 0) {
|
|
26
|
+
return uri;
|
|
27
|
+
}
|
|
28
|
+
const entries = Object.entries(query).filter(([, value]) => value !== void 0 && value !== "");
|
|
29
|
+
if (entries.length === 0) {
|
|
30
|
+
return uri;
|
|
31
|
+
}
|
|
32
|
+
const hasOrigin = uri.startsWith("http://") || uri.startsWith("https://");
|
|
33
|
+
const parsed = hasOrigin ? new URL(uri) : new URL(uri, "https://placeholder.invalid");
|
|
34
|
+
for (const [name, value] of entries) {
|
|
35
|
+
parsed.searchParams.set(name, String(value));
|
|
36
|
+
}
|
|
37
|
+
if (hasOrigin) {
|
|
38
|
+
return parsed.toString();
|
|
39
|
+
}
|
|
40
|
+
return `${parsed.pathname}${parsed.search}`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// src/merchant-client.ts
|
|
44
|
+
import {
|
|
45
|
+
MerchantBackendException,
|
|
46
|
+
joinUrl
|
|
47
|
+
} from "@mainmoneyapp/js-core";
|
|
48
|
+
function createHttp(session, http) {
|
|
49
|
+
const client = http ?? new FetchHttpClient(session.fetch, session.timeoutMs);
|
|
50
|
+
const request = async (method, path, options = {}) => {
|
|
51
|
+
const headers = { ...options.headers ?? {} };
|
|
52
|
+
if (session.clientToken !== void 0 && session.clientToken !== "") {
|
|
53
|
+
headers.Authorization ??= `Bearer ${session.clientToken}`;
|
|
54
|
+
}
|
|
55
|
+
const uri = joinUrl(session.merchantBackendUrl, path);
|
|
56
|
+
const response = await client.request(method, uri, { ...options, headers });
|
|
57
|
+
return decodeJson(response.status, response.bodyText);
|
|
58
|
+
};
|
|
59
|
+
return {
|
|
60
|
+
get: (path, query, extraHeaders) => request("GET", path, { query, headers: extraHeaders }),
|
|
61
|
+
post: (path, json, extraHeaders) => request("POST", path, { json, headers: extraHeaders })
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function decodeJson(statusCode, bodyText) {
|
|
65
|
+
let parsed = null;
|
|
66
|
+
if (bodyText !== "") {
|
|
67
|
+
try {
|
|
68
|
+
parsed = JSON.parse(bodyText);
|
|
69
|
+
} catch {
|
|
70
|
+
parsed = bodyText;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (statusCode < 200 || statusCode >= 300) {
|
|
74
|
+
throw exceptionFromBody(statusCode, parsed);
|
|
75
|
+
}
|
|
76
|
+
return unwrapEnvelope(parsed);
|
|
77
|
+
}
|
|
78
|
+
function unwrapEnvelope(parsed) {
|
|
79
|
+
if (!isPlainObject(parsed)) {
|
|
80
|
+
return parsed;
|
|
81
|
+
}
|
|
82
|
+
if ("success" in parsed && "response_data" in parsed) {
|
|
83
|
+
if (parsed.success === false) {
|
|
84
|
+
throw exceptionFromBody(400, parsed);
|
|
85
|
+
}
|
|
86
|
+
return parsed.response_data;
|
|
87
|
+
}
|
|
88
|
+
return parsed;
|
|
89
|
+
}
|
|
90
|
+
function exceptionFromBody(statusCode, parsed) {
|
|
91
|
+
if (isPlainObject(parsed)) {
|
|
92
|
+
const message = typeof parsed.message === "string" ? parsed.message : "Merchant backend request failed";
|
|
93
|
+
const responseData = parsed.response_data;
|
|
94
|
+
let errors = {};
|
|
95
|
+
if (isPlainObject(responseData) && isPlainObject(responseData.errors)) {
|
|
96
|
+
errors = responseData.errors;
|
|
97
|
+
} else if (isPlainObject(parsed.errors)) {
|
|
98
|
+
errors = parsed.errors;
|
|
99
|
+
}
|
|
100
|
+
return new MerchantBackendException(message, statusCode, errors, parsed);
|
|
101
|
+
}
|
|
102
|
+
return new MerchantBackendException("Merchant backend request failed", statusCode, {}, parsed);
|
|
103
|
+
}
|
|
104
|
+
function isPlainObject(value) {
|
|
105
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
106
|
+
}
|
|
107
|
+
export {
|
|
108
|
+
FetchHttpClient,
|
|
109
|
+
createHttp,
|
|
110
|
+
decodeJson
|
|
111
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mainmoneyapp/js-http",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.5",
|
|
4
|
+
"description": "Merchant-backend HTTP client for the MainMoney JS/TS frontend SDK",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "MainMoney SARL",
|
|
8
|
+
"homepage": "https://github.com/MainMoney-Inc/mm_aggr_js_sdk",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/MainMoney-Inc/mm_aggr_js_sdk.git",
|
|
12
|
+
"directory": "packages/http"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/MainMoney-Inc/mm_aggr_js_sdk/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"mainmoney",
|
|
19
|
+
"checkout",
|
|
20
|
+
"http"
|
|
21
|
+
],
|
|
22
|
+
"main": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"LICENSE",
|
|
33
|
+
"README.md"
|
|
34
|
+
],
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
40
|
+
"typecheck": "tsc --noEmit -p tsconfig.json"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@mainmoneyapp/js-core": "0.1.5"
|
|
44
|
+
}
|
|
45
|
+
}
|