@sudobility/consumables_client 0.0.4 → 0.0.6
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,15 +1,16 @@
|
|
|
1
|
-
import type { ConsumablePurchaseRecord, ConsumableUsageRecord } from "@sudobility/types";
|
|
1
|
+
import type { ConsumablePurchaseRecord, ConsumableUsageRecord, NetworkClient } from "@sudobility/types";
|
|
2
2
|
import type { CreditBalance } from "../types";
|
|
3
3
|
export interface ConsumablesApiClientConfig {
|
|
4
4
|
baseUrl: string;
|
|
5
|
-
|
|
5
|
+
networkClient: NetworkClient;
|
|
6
6
|
}
|
|
7
7
|
export declare class ConsumablesApiClient {
|
|
8
|
-
private baseUrl;
|
|
9
|
-
private
|
|
8
|
+
private readonly baseUrl;
|
|
9
|
+
private readonly networkClient;
|
|
10
10
|
constructor(config: ConsumablesApiClientConfig);
|
|
11
|
-
private
|
|
12
|
-
private
|
|
11
|
+
private buildUrl;
|
|
12
|
+
private get;
|
|
13
|
+
private post;
|
|
13
14
|
getBalance(): Promise<CreditBalance>;
|
|
14
15
|
recordPurchase(params: {
|
|
15
16
|
credits: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConsumablesApiClient.d.ts","sourceRoot":"","sources":["../../src/network/ConsumablesApiClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,qBAAqB,
|
|
1
|
+
{"version":3,"file":"ConsumablesApiClient.d.ts","sourceRoot":"","sources":["../../src/network/ConsumablesApiClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,qBAAqB,EACrB,aAAa,EACd,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE9C,MAAM,WAAW,0BAA0B;IACzC,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,aAAa,CAAC;CAC9B;AAOD,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAgB;gBAElC,MAAM,EAAE,0BAA0B;IAK9C,OAAO,CAAC,QAAQ;YAIF,GAAG;YAYH,IAAI;IAaZ,UAAU,IAAI,OAAO,CAAC,aAAa,CAAC;IAWpC,cAAc,CAAC,MAAM,EAAE;QAC3B,OAAO,EAAE,MAAM,CAAC;QAChB,MAAM,EAAE,MAAM,CAAC;QACf,kBAAkB,CAAC,EAAE,MAAM,CAAC;QAC5B,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB,GAAG,OAAO,CAAC,aAAa,CAAC;IAWpB,WAAW,CACf,QAAQ,CAAC,EAAE,MAAM,GAChB,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC;IAM3C,kBAAkB,CACtB,KAAK,SAAK,EACV,MAAM,SAAI,GACT,OAAO,CAAC,wBAAwB,EAAE,CAAC;IAMhC,eAAe,CACnB,KAAK,SAAK,EACV,MAAM,SAAI,GACT,OAAO,CAAC,qBAAqB,EAAE,CAAC;CAKpC"}
|
|
@@ -1,55 +1,49 @@
|
|
|
1
1
|
export class ConsumablesApiClient {
|
|
2
2
|
constructor(config) {
|
|
3
3
|
this.baseUrl = config.baseUrl.replace(/\/$/, "");
|
|
4
|
-
this.
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
this.networkClient = config.networkClient;
|
|
5
|
+
}
|
|
6
|
+
buildUrl(path) {
|
|
7
|
+
return `${this.baseUrl}/api/v1/consumables${path}`;
|
|
8
|
+
}
|
|
9
|
+
async get(path) {
|
|
10
|
+
const response = await this.networkClient.get(this.buildUrl(path));
|
|
11
|
+
if (!response.ok || !response.data) {
|
|
12
|
+
throw new Error(response.data?.error || `Request failed: ${response.status}`);
|
|
12
13
|
}
|
|
13
|
-
return
|
|
14
|
-
}
|
|
15
|
-
async
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const json = (await res.json());
|
|
20
|
-
if (!res.ok) {
|
|
21
|
-
throw new Error(json.error || `Request failed: ${res.status}`);
|
|
14
|
+
return response.data.data;
|
|
15
|
+
}
|
|
16
|
+
async post(path, body) {
|
|
17
|
+
const response = await this.networkClient.post(this.buildUrl(path), body);
|
|
18
|
+
if (!response.ok || !response.data) {
|
|
19
|
+
throw new Error(response.data?.error || `Request failed: ${response.status}`);
|
|
22
20
|
}
|
|
23
|
-
return
|
|
21
|
+
return response.data.data;
|
|
24
22
|
}
|
|
25
23
|
async getBalance() {
|
|
26
|
-
const data = await this.
|
|
24
|
+
const data = await this.get("/balance");
|
|
27
25
|
return {
|
|
28
26
|
balance: data.balance,
|
|
29
27
|
initialCredits: data.initial_credits,
|
|
30
28
|
};
|
|
31
29
|
}
|
|
32
30
|
async recordPurchase(params) {
|
|
33
|
-
const data = await this.
|
|
34
|
-
method: "POST",
|
|
35
|
-
body: JSON.stringify(params),
|
|
36
|
-
});
|
|
31
|
+
const data = await this.post("/purchase", params);
|
|
37
32
|
return {
|
|
38
33
|
balance: data.balance,
|
|
39
34
|
initialCredits: data.initial_credits,
|
|
40
35
|
};
|
|
41
36
|
}
|
|
42
37
|
async recordUsage(filename) {
|
|
43
|
-
return this.
|
|
44
|
-
|
|
45
|
-
body: JSON.stringify({ filename }),
|
|
38
|
+
return this.post("/use", {
|
|
39
|
+
filename,
|
|
46
40
|
});
|
|
47
41
|
}
|
|
48
42
|
async getPurchaseHistory(limit = 50, offset = 0) {
|
|
49
|
-
return this.
|
|
43
|
+
return this.get(`/purchases?limit=${limit}&offset=${offset}`);
|
|
50
44
|
}
|
|
51
45
|
async getUsageHistory(limit = 50, offset = 0) {
|
|
52
|
-
return this.
|
|
46
|
+
return this.get(`/usages?limit=${limit}&offset=${offset}`);
|
|
53
47
|
}
|
|
54
48
|
}
|
|
55
49
|
//# sourceMappingURL=ConsumablesApiClient.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConsumablesApiClient.js","sourceRoot":"","sources":["../../src/network/ConsumablesApiClient.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ConsumablesApiClient.js","sourceRoot":"","sources":["../../src/network/ConsumablesApiClient.ts"],"names":[],"mappings":"AAiBA,MAAM,OAAO,oBAAoB;IAI/B,YAAY,MAAkC;QAC5C,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QACjD,IAAI,CAAC,aAAa,GAAG,MAAM,CAAC,aAAa,CAAC;IAC5C,CAAC;IAEO,QAAQ,CAAC,IAAY;QAC3B,OAAO,GAAG,IAAI,CAAC,OAAO,sBAAsB,IAAI,EAAE,CAAC;IACrD,CAAC;IAEO,KAAK,CAAC,GAAG,CAAI,IAAY;QAC/B,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,GAAG,CAC3C,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CACpB,CAAC;QACF,IAAI,CAAC,QAAQ,CAAC,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;YACnC,MAAM,IAAI,KAAK,CACb,QAAQ,CAAC,IAAI,EAAE,KAAK,IAAI,mBAAmB,QAAQ,CAAC,MAAM,EAAE,CAC7D,CAAC;QACJ,CAAC;QACD,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC;IAC5B,CAAC;IAEO,KAAK,CAAC,IAAI,CAAI,IAAY,EAAE,IAAc;QAChD,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,IAAI,CAC5C,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EACnB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\n} from \"@sudobility/types\";\nimport type { CreditBalance } from \"../types\";\n\nexport interface ConsumablesApiClientConfig {\n baseUrl: string;\n networkClient: NetworkClient;\n}\n\ninterface ApiResponse<T> {\n data: T;\n error?: string;\n}\n\nexport class ConsumablesApiClient {\n private readonly baseUrl: string;\n private readonly networkClient: NetworkClient;\n\n constructor(config: ConsumablesApiClientConfig) {\n this.baseUrl = config.baseUrl.replace(/\\/$/, \"\");\n this.networkClient = config.networkClient;\n }\n\n private buildUrl(path: string): string {\n return `${this.baseUrl}/api/v1/consumables${path}`;\n }\n\n private async get<T>(path: string): Promise<T> {\n const response = await this.networkClient.get<ApiResponse<T>>(\n this.buildUrl(path),\n );\n if (!response.ok || !response.data) {\n throw new Error(\n response.data?.error || `Request failed: ${response.status}`,\n );\n }\n return response.data.data;\n }\n\n private async post<T>(path: string, body?: unknown): Promise<T> {\n const response = await this.networkClient.post<ApiResponse<T>>(\n this.buildUrl(path),\n body,\n );\n if (!response.ok || !response.data) {\n throw new Error(\n response.data?.error || `Request failed: ${response.status}`,\n );\n }\n return response.data.data;\n }\n\n async getBalance(): Promise<CreditBalance> {\n const data = await this.get<{\n balance: number;\n initial_credits: number;\n }>(\"/balance\");\n return {\n balance: data.balance,\n initialCredits: data.initial_credits,\n };\n }\n\n async recordPurchase(params: {\n credits: number;\n source: string;\n transaction_ref_id?: string;\n product_id?: string;\n price_cents?: number;\n currency?: string;\n }): Promise<CreditBalance> {\n const data = await this.post<{\n balance: number;\n initial_credits: number;\n }>(\"/purchase\", params);\n return {\n balance: data.balance,\n initialCredits: data.initial_credits,\n };\n }\n\n async recordUsage(\n filename?: string,\n ): Promise<{ balance: number; success: boolean }> {\n return this.post<{ balance: number; success: boolean }>(\"/use\", {\n filename,\n });\n }\n\n async getPurchaseHistory(\n limit = 50,\n offset = 0,\n ): Promise<ConsumablePurchaseRecord[]> {\n return this.get<ConsumablePurchaseRecord[]>(\n `/purchases?limit=${limit}&offset=${offset}`,\n );\n }\n\n async getUsageHistory(\n limit = 50,\n offset = 0,\n ): Promise<ConsumableUsageRecord[]> {\n return this.get<ConsumableUsageRecord[]>(\n `/usages?limit=${limit}&offset=${offset}`,\n );\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sudobility/consumables_client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "Cross-platform consumable credits client with RevenueCat adapter pattern",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"prepublishOnly": "bun run build"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@sudobility/types": "^1.9.
|
|
32
|
+
"@sudobility/types": "^1.9.53",
|
|
33
33
|
"react": "^18.0.0 || ^19.0.0",
|
|
34
34
|
"@revenuecat/purchases-js": "^1.0.0",
|
|
35
35
|
"react-native-purchases": ">=7.0.0"
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@eslint/js": "^10.0.1",
|
|
47
47
|
"@revenuecat/purchases-js": "^1.1.3",
|
|
48
|
-
"@sudobility/types": "^1.9.
|
|
48
|
+
"@sudobility/types": "^1.9.53",
|
|
49
49
|
"@types/bun": "^1.2.8",
|
|
50
50
|
"@types/node": "^22.0.0",
|
|
51
51
|
"@types/react": "^19.2.5",
|