@lacagnottedesproches/lcdp-js-lite 1.6.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/.babelrc +3 -0
- package/.eslintrc.json +3 -0
- package/.github/workflows/node.js.yml +27 -0
- package/.github/workflows/test.js.yml +28 -0
- package/.prettierignore +5 -0
- package/.prettierrc.json +2 -0
- package/LICENSE +674 -0
- package/README.md +9 -0
- package/__tests__/api.test.ts +171 -0
- package/__tests__/index.test.ts +228 -0
- package/__tests__/types.runtime.test.ts +16 -0
- package/__tests__/types.test-d.ts +87 -0
- package/dist/api.d.ts +15 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +591 -0
- package/dist/index.min.js +3 -0
- package/dist/index.mjs +588 -0
- package/dist/index.umd.js +597 -0
- package/dist/index.umd.min.js +3 -0
- package/dist/types.d.ts +269 -0
- package/jest.config.js +6 -0
- package/jest.setup.ts +39 -0
- package/package.json +57 -0
- package/rollup.config.cjs +55 -0
- package/src/api.ts +143 -0
- package/src/index.ts +151 -0
- package/src/types.ts +320 -0
- package/tsconfig.json +15 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { Stripe } from "@stripe/stripe-js";
|
|
2
|
+
import { UUID } from "crypto";
|
|
3
|
+
export declare enum AfinaError {
|
|
4
|
+
Verify = "Fail to verify origin request",
|
|
5
|
+
Stripe = "Stripe is not available",
|
|
6
|
+
ClientSide = "Should be on Client Side",
|
|
7
|
+
Key = "A publishable key is mandatory"
|
|
8
|
+
}
|
|
9
|
+
export declare enum TokenObjectType {
|
|
10
|
+
Account = "account",
|
|
11
|
+
Pot = "pot",
|
|
12
|
+
Wizard = "wizard"
|
|
13
|
+
}
|
|
14
|
+
export interface Country {
|
|
15
|
+
id: string;
|
|
16
|
+
name: string;
|
|
17
|
+
}
|
|
18
|
+
export interface Category {
|
|
19
|
+
id: string;
|
|
20
|
+
name: string;
|
|
21
|
+
slug: string;
|
|
22
|
+
icon: string | null;
|
|
23
|
+
icon_url: string | null;
|
|
24
|
+
}
|
|
25
|
+
export interface TokenObject {
|
|
26
|
+
id: string;
|
|
27
|
+
type: TokenObjectType;
|
|
28
|
+
client_ip: string;
|
|
29
|
+
}
|
|
30
|
+
interface Pagination {
|
|
31
|
+
has_next: boolean;
|
|
32
|
+
has_prev: boolean;
|
|
33
|
+
page: number;
|
|
34
|
+
pages: number;
|
|
35
|
+
per_page: number;
|
|
36
|
+
total: number;
|
|
37
|
+
}
|
|
38
|
+
export interface ProfitAccount {
|
|
39
|
+
entity_name: string | null;
|
|
40
|
+
entity_url: string | null;
|
|
41
|
+
}
|
|
42
|
+
export interface Pot {
|
|
43
|
+
id: string;
|
|
44
|
+
date_created_ts: number;
|
|
45
|
+
slug: string;
|
|
46
|
+
name: string;
|
|
47
|
+
date_end_ts: number | null;
|
|
48
|
+
objectif: number | null;
|
|
49
|
+
status_online: boolean;
|
|
50
|
+
status_public: boolean;
|
|
51
|
+
status_closed: boolean;
|
|
52
|
+
amount_collected_hidden: boolean;
|
|
53
|
+
public_pot_url: string;
|
|
54
|
+
public_donation_url: string;
|
|
55
|
+
stat_amount_donation: number | null;
|
|
56
|
+
stat_nb_donation: number;
|
|
57
|
+
profit_acct: ProfitAccount | null;
|
|
58
|
+
profit_ghost: ProfitAccount | null;
|
|
59
|
+
description: string | null;
|
|
60
|
+
video: string | null;
|
|
61
|
+
visual_main_url: string | null;
|
|
62
|
+
visual_secondary_url: string | null;
|
|
63
|
+
with_tax_receipt: boolean;
|
|
64
|
+
}
|
|
65
|
+
export interface PotsPagination extends Pagination {
|
|
66
|
+
items: Pot[];
|
|
67
|
+
}
|
|
68
|
+
export interface PayloadAccount {
|
|
69
|
+
email: string;
|
|
70
|
+
password?: string | null;
|
|
71
|
+
first_name: string;
|
|
72
|
+
last_name: string;
|
|
73
|
+
birth_date: Date;
|
|
74
|
+
address: string;
|
|
75
|
+
postcode: string;
|
|
76
|
+
city: string;
|
|
77
|
+
phone_number: string;
|
|
78
|
+
country_code: string;
|
|
79
|
+
tags?: string[] | undefined;
|
|
80
|
+
}
|
|
81
|
+
interface PayloadPotBase {
|
|
82
|
+
name: string;
|
|
83
|
+
category_id: string;
|
|
84
|
+
amount_target?: number | undefined;
|
|
85
|
+
ts_end?: number | undefined;
|
|
86
|
+
status_public?: boolean | undefined;
|
|
87
|
+
total_amount_hidden?: boolean | undefined;
|
|
88
|
+
description?: string | undefined;
|
|
89
|
+
assoid?: string | undefined;
|
|
90
|
+
}
|
|
91
|
+
export interface PayloadPot extends PayloadPotBase {
|
|
92
|
+
acct_id: string;
|
|
93
|
+
}
|
|
94
|
+
export interface PayloadPotUpdate {
|
|
95
|
+
pot_id: UUID;
|
|
96
|
+
name?: string | undefined;
|
|
97
|
+
category_id?: UUID | undefined;
|
|
98
|
+
objectif?: number | undefined;
|
|
99
|
+
date_end_ts?: number | undefined;
|
|
100
|
+
status_public?: boolean | undefined;
|
|
101
|
+
amount_collected_hidden?: boolean | undefined;
|
|
102
|
+
description?: string | undefined;
|
|
103
|
+
}
|
|
104
|
+
export interface PayloadAccountAndPot extends PayloadAccount, PayloadPotBase {
|
|
105
|
+
}
|
|
106
|
+
export interface DataDonation {
|
|
107
|
+
first_name: string;
|
|
108
|
+
last_name: string;
|
|
109
|
+
email: string;
|
|
110
|
+
pot_id: UUID;
|
|
111
|
+
return_url: string;
|
|
112
|
+
message?: string | null | undefined;
|
|
113
|
+
stay_anonymous?: boolean | null | undefined;
|
|
114
|
+
hide_amount_donation?: boolean | null | undefined;
|
|
115
|
+
is_newsletter?: boolean | null | undefined;
|
|
116
|
+
address?: string | null | undefined;
|
|
117
|
+
postcode?: string | null | undefined;
|
|
118
|
+
city?: string | null | undefined;
|
|
119
|
+
country?: string | null | undefined;
|
|
120
|
+
org_name?: string | null | undefined;
|
|
121
|
+
org_siren?: string | null | undefined;
|
|
122
|
+
org_status?: string | null | undefined;
|
|
123
|
+
}
|
|
124
|
+
export interface PayloadDonation extends DataDonation {
|
|
125
|
+
confirmation_token_id: string;
|
|
126
|
+
idempotency_key: string;
|
|
127
|
+
amount_donation: number;
|
|
128
|
+
amount_tip: number;
|
|
129
|
+
}
|
|
130
|
+
export interface DonationResult {
|
|
131
|
+
error?: string;
|
|
132
|
+
status: string | "requires_action";
|
|
133
|
+
client_secret: string;
|
|
134
|
+
donation_id?: UUID;
|
|
135
|
+
}
|
|
136
|
+
export interface AccountCreated {
|
|
137
|
+
acct_id: string;
|
|
138
|
+
first_name: string;
|
|
139
|
+
last_name: string;
|
|
140
|
+
email: string;
|
|
141
|
+
}
|
|
142
|
+
export interface PotCreated {
|
|
143
|
+
pot_id: string;
|
|
144
|
+
name: string;
|
|
145
|
+
slug: string;
|
|
146
|
+
}
|
|
147
|
+
export interface AccountAndPotCreated {
|
|
148
|
+
account: AccountCreated;
|
|
149
|
+
pot: PotCreated;
|
|
150
|
+
}
|
|
151
|
+
export interface StatMetric {
|
|
152
|
+
nb_pots: number;
|
|
153
|
+
amount_donations: number;
|
|
154
|
+
amount_moy_donations: number;
|
|
155
|
+
nb_donations: number;
|
|
156
|
+
}
|
|
157
|
+
export interface StatMetricTips {
|
|
158
|
+
nb_tips: number;
|
|
159
|
+
amount: number;
|
|
160
|
+
amount_real: number;
|
|
161
|
+
}
|
|
162
|
+
export interface StatDashboard {
|
|
163
|
+
total: StatMetric;
|
|
164
|
+
date_range: StatMetric;
|
|
165
|
+
tips?: {
|
|
166
|
+
total: StatMetricTips;
|
|
167
|
+
date_range: StatMetricTips;
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
export interface StatsPot {
|
|
171
|
+
id: string;
|
|
172
|
+
date_created_ts: number;
|
|
173
|
+
slug: string;
|
|
174
|
+
name: string;
|
|
175
|
+
status_online: boolean;
|
|
176
|
+
status_public: boolean;
|
|
177
|
+
status_closed: boolean;
|
|
178
|
+
owner_first_name: string | null;
|
|
179
|
+
owner_last_name: string | null;
|
|
180
|
+
tags?: string | null;
|
|
181
|
+
nb_donations: number;
|
|
182
|
+
amount_donations: number | null;
|
|
183
|
+
amount_moy_donations: number | null;
|
|
184
|
+
nb_tips?: number;
|
|
185
|
+
amount_tips?: number;
|
|
186
|
+
amount_real_tips?: number;
|
|
187
|
+
}
|
|
188
|
+
export interface StatsPotsPagination extends Pagination {
|
|
189
|
+
items: StatsPot[];
|
|
190
|
+
}
|
|
191
|
+
export interface Association {
|
|
192
|
+
id: string;
|
|
193
|
+
nom: string;
|
|
194
|
+
recu_fiscal: 0 | 1;
|
|
195
|
+
mise_en_avant: 0 | 1;
|
|
196
|
+
categories?: string[];
|
|
197
|
+
description_courte?: string;
|
|
198
|
+
link_logo: string;
|
|
199
|
+
alt_logo: string;
|
|
200
|
+
link_cta_pot: string;
|
|
201
|
+
lien_page_wordpress: string;
|
|
202
|
+
assoid: string;
|
|
203
|
+
}
|
|
204
|
+
export interface Associations {
|
|
205
|
+
hits: Association[];
|
|
206
|
+
offset: number;
|
|
207
|
+
limit: number;
|
|
208
|
+
query: string;
|
|
209
|
+
}
|
|
210
|
+
export interface PayloadQueryAssociations {
|
|
211
|
+
q?: string;
|
|
212
|
+
limit?: number;
|
|
213
|
+
offset?: number;
|
|
214
|
+
filter?: string[][];
|
|
215
|
+
}
|
|
216
|
+
export interface AssociationCategory {
|
|
217
|
+
id: string;
|
|
218
|
+
name: string;
|
|
219
|
+
picto?: string;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Represents available features from Afina API.
|
|
223
|
+
*/
|
|
224
|
+
export interface LCDP {
|
|
225
|
+
stripe: Stripe | undefined | null;
|
|
226
|
+
init: () => Promise<LCDP>;
|
|
227
|
+
createTokenAccount: (payload: PayloadAccount) => Promise<TokenObject>;
|
|
228
|
+
createTokenPot: (payload: PayloadPot) => Promise<TokenObject>;
|
|
229
|
+
createTokenAccountAndPot: (payload: PayloadAccountAndPot) => Promise<TokenObject>;
|
|
230
|
+
createTokenPotUpdate: (payload: PayloadPotUpdate) => Promise<TokenObject>;
|
|
231
|
+
createDonation: (payload: PayloadDonation) => Promise<DonationResult>;
|
|
232
|
+
getAvailableCountries: () => Promise<Country[]>;
|
|
233
|
+
getCategories: () => Promise<Category[]>;
|
|
234
|
+
getPots: (params?: ParamsPagination) => Promise<PotsPagination>;
|
|
235
|
+
getPot: (potId: string) => Promise<Pot>;
|
|
236
|
+
getStatsDashboard: (ts_start?: number, ts_end?: number) => Promise<StatDashboard>;
|
|
237
|
+
getStatsPots: (ts_start?: number, ts_end?: number, page?: number, per_page?: number) => Promise<StatsPotsPagination>;
|
|
238
|
+
retrieveAssociations: (payload?: PayloadQueryAssociations) => Promise<Associations>;
|
|
239
|
+
getAssociation: (id: string) => Promise<Association>;
|
|
240
|
+
getAssociationCategories: () => Promise<AssociationCategory[]>;
|
|
241
|
+
}
|
|
242
|
+
export interface LCDPApi {
|
|
243
|
+
createAccount: (payload: TokenObject) => Promise<AccountCreated>;
|
|
244
|
+
createPot: (payload: TokenObject) => Promise<PotCreated>;
|
|
245
|
+
createAccountAndPot: (payload: TokenObject) => Promise<AccountAndPotCreated>;
|
|
246
|
+
updatePot: (payload: TokenObject) => Promise<void>;
|
|
247
|
+
}
|
|
248
|
+
export type LCDPOpts = {
|
|
249
|
+
api_url?: string;
|
|
250
|
+
};
|
|
251
|
+
export interface LCDPConstructor {
|
|
252
|
+
(lcdpPubKey: string, options?: LCDPOpts): LCDP;
|
|
253
|
+
}
|
|
254
|
+
export type LoadLCDP = (...args: Parameters<LCDPConstructor>) => Promise<LCDP | null>;
|
|
255
|
+
export interface ParamsPagination {
|
|
256
|
+
page?: number;
|
|
257
|
+
per_page?: number;
|
|
258
|
+
}
|
|
259
|
+
declare global {
|
|
260
|
+
interface Window {
|
|
261
|
+
lcdp?: {
|
|
262
|
+
Afina: LCDPConstructor;
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
export interface DataAccount extends Omit<PayloadAccount, "birth_date"> {
|
|
267
|
+
birth_date: number;
|
|
268
|
+
}
|
|
269
|
+
export {};
|
package/jest.config.js
ADDED
package/jest.setup.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import fetchMock from "jest-fetch-mock";
|
|
2
|
+
|
|
3
|
+
fetchMock.enableMocks();
|
|
4
|
+
|
|
5
|
+
// Utility to safely assign to globalThis
|
|
6
|
+
const setGlobalProp = (key: string, value: unknown) => {
|
|
7
|
+
(globalThis as any)[key] = value;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
setGlobalProp("fetch", fetchMock);
|
|
11
|
+
|
|
12
|
+
const mockScript = {
|
|
13
|
+
src: "",
|
|
14
|
+
addEventListener: jest.fn(),
|
|
15
|
+
removeEventListener: jest.fn(),
|
|
16
|
+
parentNode: { removeChild: jest.fn() },
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const mockDocument = {
|
|
20
|
+
head: {
|
|
21
|
+
appendChild: jest.fn(),
|
|
22
|
+
},
|
|
23
|
+
body: {
|
|
24
|
+
appendChild: jest.fn(),
|
|
25
|
+
},
|
|
26
|
+
createElement: jest.fn((tag: string) => {
|
|
27
|
+
if (tag === "script") return mockScript;
|
|
28
|
+
return { id: "", appendChild: jest.fn() };
|
|
29
|
+
}),
|
|
30
|
+
querySelectorAll: jest.fn(() => []),
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// window mock
|
|
34
|
+
const mockWindow: Record<string, any> = {
|
|
35
|
+
lcdp: undefined,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
setGlobalProp("window", mockWindow);
|
|
39
|
+
setGlobalProp("document", mockDocument);
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lacagnottedesproches/lcdp-js-lite",
|
|
3
|
+
"version": "1.6.0",
|
|
4
|
+
"description": "LCDP JS Lite",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "rm -rf dist/ && rollup -c",
|
|
7
|
+
"dev": "rollup -c -w",
|
|
8
|
+
"test": "jest --verbose",
|
|
9
|
+
"test:watch": "jest --watch",
|
|
10
|
+
"test:coverage": "jest --verbose --coverage",
|
|
11
|
+
"test:coverage:watch": "jest --coverage --watch",
|
|
12
|
+
"test:ci": "jest --ci --coverage --watchAll=false",
|
|
13
|
+
"test:unit": "jest --testPathPattern=unit",
|
|
14
|
+
"test:integration": "jest --testPathPattern=integration",
|
|
15
|
+
"test:types": "tsd",
|
|
16
|
+
"test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand",
|
|
17
|
+
"test:update-snapshots": "jest --updateSnapshot",
|
|
18
|
+
"test:clear-cache": "jest --clearCache"
|
|
19
|
+
},
|
|
20
|
+
"main": "dist/index.js",
|
|
21
|
+
"module": "dist/index.mjs",
|
|
22
|
+
"browser": "dist/index.umd.js",
|
|
23
|
+
"browser:min": "dist/index.umd.min.js",
|
|
24
|
+
"types": "dist/index.d.ts",
|
|
25
|
+
"typings": "dist/index.d.ts",
|
|
26
|
+
"keywords": [],
|
|
27
|
+
"author": "La Cagnotte des Proches (https://lacagnottedesproches.fr)",
|
|
28
|
+
"license": "GPL-3.0-only",
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@babel/core": "^7.24.6",
|
|
31
|
+
"@babel/preset-env": "^7.24.6",
|
|
32
|
+
"@jest/globals": "^29.7.0",
|
|
33
|
+
"@rollup/plugin-babel": "^6.1.0",
|
|
34
|
+
"@rollup/plugin-commonjs": "^29.0.0",
|
|
35
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
36
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
37
|
+
"@rollup/plugin-replace": "^6.0.3",
|
|
38
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
39
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
40
|
+
"@types/jest": "^29.5.14",
|
|
41
|
+
"@types/node": "^20.12.12",
|
|
42
|
+
"autoprefixer": "^10.4.19",
|
|
43
|
+
"generate-changelog": "^1.8.0",
|
|
44
|
+
"jest": "^29.7.0",
|
|
45
|
+
"jest-fetch-mock": "^3.0.3",
|
|
46
|
+
"prettier": "^3.2.5",
|
|
47
|
+
"rollup": "^4.53.3",
|
|
48
|
+
"ts-jest": "^29.4.5",
|
|
49
|
+
"tsd": "^0.33.0",
|
|
50
|
+
"tslib": "^2.8.1",
|
|
51
|
+
"typedoc": "^0.27.4",
|
|
52
|
+
"typescript": "^5.4.5"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"@stripe/stripe-js": "^3.0.0"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const { babel } = require("@rollup/plugin-babel");
|
|
2
|
+
const commonjs = require("@rollup/plugin-commonjs");
|
|
3
|
+
const { nodeResolve } = require("@rollup/plugin-node-resolve");
|
|
4
|
+
const typescript = require("@rollup/plugin-typescript");
|
|
5
|
+
const terser = require("@rollup/plugin-terser");
|
|
6
|
+
const replace = require("@rollup/plugin-replace");
|
|
7
|
+
const json = require("@rollup/plugin-json");
|
|
8
|
+
const pkg = require("./package.json");
|
|
9
|
+
|
|
10
|
+
const plugins = [
|
|
11
|
+
typescript(),
|
|
12
|
+
json(),
|
|
13
|
+
commonjs(),
|
|
14
|
+
nodeResolve(),
|
|
15
|
+
babel({
|
|
16
|
+
extensions: [".ts", ".js"],
|
|
17
|
+
babelHelpers: "bundled",
|
|
18
|
+
}),
|
|
19
|
+
replace({
|
|
20
|
+
preventAssignment: true,
|
|
21
|
+
"process.env.NODE_ENV": JSON.stringify("production"),
|
|
22
|
+
}),
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
module.exports = {
|
|
26
|
+
input: "src/index.ts",
|
|
27
|
+
output: [
|
|
28
|
+
{
|
|
29
|
+
format: "cjs",
|
|
30
|
+
file: pkg.main,
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
format: "es",
|
|
34
|
+
file: pkg.module,
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: "lcdpjs",
|
|
38
|
+
format: "iife",
|
|
39
|
+
file: "dist/index.min.js",
|
|
40
|
+
plugins: [terser()],
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: "lcdpjs",
|
|
44
|
+
format: "umd",
|
|
45
|
+
file: pkg.browser,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: "lcdpjs",
|
|
49
|
+
format: "umd",
|
|
50
|
+
file: pkg["browser:min"],
|
|
51
|
+
plugins: [terser()],
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
plugins,
|
|
55
|
+
};
|
package/src/api.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AccountCreated,
|
|
3
|
+
LCDPApi,
|
|
4
|
+
PotCreated,
|
|
5
|
+
TokenObject,
|
|
6
|
+
AccountAndPotCreated,
|
|
7
|
+
} from "./types";
|
|
8
|
+
|
|
9
|
+
interface LCDPApiResponse {
|
|
10
|
+
[key: string]: string | LCDPApiResponse;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class LCDPRequest implements LCDPApi {
|
|
14
|
+
_apiToken: string;
|
|
15
|
+
_apiUrl: string;
|
|
16
|
+
|
|
17
|
+
constructor(secret: string, apiUrl: string) {
|
|
18
|
+
if (typeof window !== "undefined") {
|
|
19
|
+
throw new Error("Must be used server side");
|
|
20
|
+
}
|
|
21
|
+
this._apiToken = secret;
|
|
22
|
+
this._apiUrl = apiUrl;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
requestLCDPApi = (
|
|
26
|
+
endpoint: string,
|
|
27
|
+
payload: TokenObject,
|
|
28
|
+
authToken: string,
|
|
29
|
+
customMethod: "POST" | "PATCH" = "POST",
|
|
30
|
+
): Promise<LCDPApiResponse | void> => {
|
|
31
|
+
if (typeof window !== "undefined") {
|
|
32
|
+
throw new Error("Must be used server side");
|
|
33
|
+
}
|
|
34
|
+
return new Promise(async (resolve, reject) => {
|
|
35
|
+
const url = `${this._apiUrl}/${endpoint}`;
|
|
36
|
+
try {
|
|
37
|
+
const response = await fetch(url, {
|
|
38
|
+
method: customMethod,
|
|
39
|
+
headers: {
|
|
40
|
+
"Content-Type": "application/json",
|
|
41
|
+
"User-Agent": "lcdp/v1",
|
|
42
|
+
Authorization: `Bearer ${authToken}`,
|
|
43
|
+
},
|
|
44
|
+
body: JSON.stringify(payload),
|
|
45
|
+
});
|
|
46
|
+
const status = response.status;
|
|
47
|
+
if (status >= 400) {
|
|
48
|
+
const { detail } = await response.json();
|
|
49
|
+
reject(detail);
|
|
50
|
+
} else if (status === 204) {
|
|
51
|
+
resolve();
|
|
52
|
+
} else {
|
|
53
|
+
const data = await response.json();
|
|
54
|
+
resolve(data);
|
|
55
|
+
}
|
|
56
|
+
} catch (error) {
|
|
57
|
+
reject(error);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
createAccount = async (payload: TokenObject): Promise<AccountCreated> => {
|
|
63
|
+
return new Promise(async (resolve, reject) => {
|
|
64
|
+
const responsePromise = this.requestLCDPApi(
|
|
65
|
+
"accounts",
|
|
66
|
+
payload,
|
|
67
|
+
this._apiToken,
|
|
68
|
+
);
|
|
69
|
+
return responsePromise
|
|
70
|
+
.then((data) => {
|
|
71
|
+
try {
|
|
72
|
+
// TODO: may be improved
|
|
73
|
+
resolve(data as unknown as AccountCreated);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
reject(error);
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
.catch((reason) => reject(reason));
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
createPot = async (payload: TokenObject): Promise<PotCreated> => {
|
|
83
|
+
return new Promise(async (resolve, reject) => {
|
|
84
|
+
const responsePromise = this.requestLCDPApi(
|
|
85
|
+
"pots",
|
|
86
|
+
payload,
|
|
87
|
+
this._apiToken,
|
|
88
|
+
);
|
|
89
|
+
return responsePromise
|
|
90
|
+
.then((data) => {
|
|
91
|
+
try {
|
|
92
|
+
// TODO: may be improved
|
|
93
|
+
resolve(data as unknown as PotCreated);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
reject(error);
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
.catch((reason) => reject(reason));
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
createAccountAndPot = async (
|
|
103
|
+
payload: TokenObject,
|
|
104
|
+
): Promise<AccountAndPotCreated> => {
|
|
105
|
+
return new Promise(async (resolve, reject) => {
|
|
106
|
+
const responsePromise = this.requestLCDPApi(
|
|
107
|
+
"wizards",
|
|
108
|
+
payload,
|
|
109
|
+
this._apiToken,
|
|
110
|
+
);
|
|
111
|
+
return responsePromise
|
|
112
|
+
.then((data) => {
|
|
113
|
+
try {
|
|
114
|
+
// TODO: may be improved
|
|
115
|
+
resolve(data as unknown as AccountAndPotCreated);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
reject(error);
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
.catch((reason) => reject(reason));
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
updatePot = async (payload: TokenObject): Promise<void> => {
|
|
125
|
+
return new Promise(async (resolve, reject) => {
|
|
126
|
+
const responsePromise = this.requestLCDPApi(
|
|
127
|
+
"pots",
|
|
128
|
+
payload,
|
|
129
|
+
this._apiToken,
|
|
130
|
+
"PATCH",
|
|
131
|
+
);
|
|
132
|
+
return responsePromise
|
|
133
|
+
.then((_) => {
|
|
134
|
+
try {
|
|
135
|
+
resolve();
|
|
136
|
+
} catch (error) {
|
|
137
|
+
reject(error);
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
.catch((reason) => reject(reason));
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
}
|