@tapi-dev/sdk 0.1.5 → 0.1.7

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.
@@ -1,3 +1,4 @@
1
+ import { TapiError } from "./errors.js";
1
2
  export class WebsiteApisResource {
2
3
  http;
3
4
  constructor(http) {
@@ -7,6 +8,36 @@ export class WebsiteApisResource {
7
8
  const { apiName, requestKey } = splitApiRequest(apiRequest);
8
9
  return this.http.post(`/api/sdk/v1/website-apis/${encodeURIComponent(apiName)}/requests/${encodeURIComponent(requestKey)}/runs`, request);
9
10
  }
11
+ quoteCloud(apiRequest, request = {}) {
12
+ const { apiName, requestKey } = splitApiRequest(apiRequest);
13
+ return this.http.post(`/api/sdk/v1/website-apis/${encodeURIComponent(apiName)}/requests/${encodeURIComponent(requestKey)}/cloud/quote`, request);
14
+ }
15
+ runCloud(apiRequest, request = {}) {
16
+ return this.runCloudBatch(apiRequest, {
17
+ inputs: [request.inputs ?? {}],
18
+ cloud: request.cloud,
19
+ user: request.user,
20
+ payment: request.payment,
21
+ site: typeof request.site === "string" ? request.site : undefined,
22
+ idempotencyKey: request.idempotencyKey,
23
+ });
24
+ }
25
+ async runCloudBatch(apiRequest, request) {
26
+ const { apiName, requestKey } = splitApiRequest(apiRequest);
27
+ try {
28
+ return await this.http.post(`/api/sdk/v1/website-apis/${encodeURIComponent(apiName)}/requests/${encodeURIComponent(requestKey)}/cloud/batch`, request);
29
+ }
30
+ catch (error) {
31
+ if (error instanceof TapiError && error.status === 402 && isRecord(error.details)) {
32
+ return cloudPaymentRequiredRun(error.details);
33
+ }
34
+ throw error;
35
+ }
36
+ }
37
+ describe(apiRequest) {
38
+ const { apiName, requestKey } = splitApiRequest(apiRequest);
39
+ return this.http.get(`/api/sdk/v1/website-apis/${encodeURIComponent(apiName)}/requests/${encodeURIComponent(requestKey)}`);
40
+ }
10
41
  }
11
42
  function splitApiRequest(value) {
12
43
  const text = value.trim();
@@ -19,3 +50,33 @@ function splitApiRequest(value) {
19
50
  requestKey: text.slice(index + 1),
20
51
  };
21
52
  }
53
+ function cloudPaymentRequiredRun(detail) {
54
+ const run = isRecord(detail.run) ? detail.run : {};
55
+ const quote = isRecord(detail.quote) ? detail.quote : undefined;
56
+ return {
57
+ ...run,
58
+ id: typeof run.id === "string" ? run.id : "",
59
+ status: "payment_required",
60
+ quote,
61
+ user: cloudRunUser(detail.user) ?? cloudRunUser(run.user),
62
+ paymentRequired: true,
63
+ checkoutUrl: typeof detail.checkoutUrl === "string" ? detail.checkoutUrl : null,
64
+ checkoutSessionId: typeof detail.checkoutSessionId === "string" ? detail.checkoutSessionId : null,
65
+ paymentProvider: typeof detail.paymentProvider === "string" ? detail.paymentProvider : undefined,
66
+ paymentProviderEnabled: typeof detail.paymentProviderEnabled === "boolean" ? detail.paymentProviderEnabled : undefined,
67
+ availableBalanceCents: typeof detail.availableBalanceCents === "number" ? detail.availableBalanceCents : undefined,
68
+ requiredBalanceCents: typeof detail.requiredBalanceCents === "number" ? detail.requiredBalanceCents : undefined,
69
+ };
70
+ }
71
+ function cloudRunUser(value) {
72
+ if (!isRecord(value) || typeof value.externalUserId !== "string") {
73
+ return undefined;
74
+ }
75
+ return {
76
+ externalUserId: value.externalUserId,
77
+ email: typeof value.email === "string" ? value.email : undefined,
78
+ };
79
+ }
80
+ function isRecord(value) {
81
+ return typeof value === "object" && value !== null && !Array.isArray(value);
82
+ }
package/package.json CHANGED
@@ -1,36 +1,36 @@
1
- {
2
- "name": "@tapi-dev/sdk",
3
- "version": "0.1.5",
4
- "type": "module",
5
- "main": "./dist/index.js",
6
- "types": "./dist/index.d.ts",
7
- "bin": {
8
- "tapi": "dist/cli.js"
9
- },
10
- "exports": {
11
- ".": {
12
- "types": "./dist/index.d.ts",
13
- "import": "./dist/index.js"
14
- }
15
- },
16
- "engines": {
17
- "node": ">=18"
18
- },
19
- "files": [
20
- "dist"
21
- ],
22
- "scripts": {
23
- "build": "tsc -p tsconfig.json",
24
- "test": "vitest run",
25
- "prepack": "npm run build",
26
- "prepublishOnly": "npm test && npm run build"
27
- },
28
- "publishConfig": {
29
- "access": "public"
30
- },
31
- "devDependencies": {
32
- "@types/node": "^20.0.0",
33
- "typescript": "^5.5.0",
34
- "vitest": "^2.0.0"
35
- }
36
- }
1
+ {
2
+ "name": "@tapi-dev/sdk",
3
+ "version": "0.1.7",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "bin": {
8
+ "tapi": "dist/cli.js"
9
+ },
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js"
14
+ }
15
+ },
16
+ "engines": {
17
+ "node": ">=18"
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "scripts": {
23
+ "build": "tsc -p tsconfig.json",
24
+ "test": "vitest run",
25
+ "prepack": "npm run build",
26
+ "prepublishOnly": "npm test && npm run build"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "^20.0.0",
33
+ "typescript": "^5.5.0",
34
+ "vitest": "^2.0.0"
35
+ }
36
+ }