@abiyz/billing 0.0.0-stage → 0.2.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/LICENSE +99 -0
- package/README.md +121 -2
- package/index.d.ts +7518 -0
- package/index.js +52 -0
- package/package.json +23 -4
package/index.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const COMPANY_NAME = "Abiyz";
|
|
2
|
+
const TENANT_DOMAIN = "abiyz.co";
|
|
3
|
+
`${COMPANY_NAME.toLowerCase().toUpperCase()}${"cloud".toUpperCase()}`;
|
|
4
|
+
const BILLING_RUNTIME_URL = `https://api.billing.${TENANT_DOMAIN}`;
|
|
5
|
+
var BillingError = class extends Error {
|
|
6
|
+
status;
|
|
7
|
+
problem;
|
|
8
|
+
constructor(status, problem) {
|
|
9
|
+
super(problem === null ? `Billing answered ${String(status)}` : `Billing answered ${String(status)} ${problem.code}: ${problem.title}`);
|
|
10
|
+
this.name = "BillingError";
|
|
11
|
+
this.status = status;
|
|
12
|
+
this.problem = problem;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
const createBillingRuntimeClient = (options) => {
|
|
16
|
+
const base = (options.baseUrl ?? BILLING_RUNTIME_URL).replace(/\/+$/u, "");
|
|
17
|
+
const send = options.fetch ?? globalThis.fetch;
|
|
18
|
+
const post = async (path, body, idempotencyKey) => {
|
|
19
|
+
const headers = {
|
|
20
|
+
authorization: `Bearer ${options.key}`,
|
|
21
|
+
accept: "application/json"
|
|
22
|
+
};
|
|
23
|
+
if (body !== void 0) headers["content-type"] = "application/json";
|
|
24
|
+
if (idempotencyKey !== void 0) headers["idempotency-key"] = idempotencyKey;
|
|
25
|
+
const response = await send(`${base}${path}`, {
|
|
26
|
+
method: "POST",
|
|
27
|
+
headers,
|
|
28
|
+
...body === void 0 ? {} : { body: JSON.stringify(body) }
|
|
29
|
+
});
|
|
30
|
+
const text = await response.text();
|
|
31
|
+
if (!response.ok) {
|
|
32
|
+
let problem;
|
|
33
|
+
try {
|
|
34
|
+
problem = JSON.parse(text);
|
|
35
|
+
} catch {
|
|
36
|
+
problem = null;
|
|
37
|
+
}
|
|
38
|
+
throw new BillingError(response.status, problem);
|
|
39
|
+
}
|
|
40
|
+
return JSON.parse(text);
|
|
41
|
+
};
|
|
42
|
+
const hold = (id) => `/v1/holds/${encodeURIComponent(id)}`;
|
|
43
|
+
return {
|
|
44
|
+
attach: (body, call) => post("/v1/attach", body, call?.idempotencyKey),
|
|
45
|
+
check: (body) => post("/v1/check", body, void 0),
|
|
46
|
+
track: (body, call) => post("/v1/track", body, call.idempotencyKey),
|
|
47
|
+
hold: (body, call) => post("/v1/holds", body, call.idempotencyKey),
|
|
48
|
+
settle: (id, body, call) => post(`${hold(id)}/settle`, body, call.idempotencyKey),
|
|
49
|
+
release: (id, call) => post(`${hold(id)}/release`, void 0, call.idempotencyKey)
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
export { BILLING_RUNTIME_URL, BillingError, createBillingRuntimeClient };
|
package/package.json
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@abiyz/billing",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Abiyz Billing for your application's server: charge before the work and settle after it, check a balance or a feature, and sell credits, with the management API's types.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./index.d.ts",
|
|
9
|
+
"default": "./index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"types": "./index.d.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"index.js",
|
|
15
|
+
"index.d.ts",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"dependencies": {},
|
|
21
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
}
|
|
25
|
+
}
|