@moonbase.sh/api 0.1.109

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/dist/index.js ADDED
@@ -0,0 +1,346 @@
1
+ // src/activationRequests/schemas.ts
2
+ import { z as z5 } from "zod";
3
+
4
+ // src/customers/schemas.ts
5
+ import { z } from "zod";
6
+ var addressSchema = z.object({
7
+ countryCode: z.string(),
8
+ streetAddress1: z.string(),
9
+ streetAddress2: z.string().nullable(),
10
+ locality: z.string().nullable(),
11
+ region: z.string().nullable(),
12
+ postCode: z.string()
13
+ });
14
+ var communicationPreferencesSchema = z.object({
15
+ newsletterOptIn: z.boolean()
16
+ // productUpdatesOptIn: z.boolean(), // TODO: Enable when relevant
17
+ });
18
+ var customerSchema = z.object({
19
+ id: z.string(),
20
+ name: z.string(),
21
+ businessName: z.string().nullable(),
22
+ taxId: z.string().nullable(),
23
+ email: z.string(),
24
+ numberOfLicenses: z.number(),
25
+ numberOfTrials: z.number(),
26
+ emailConfirmed: z.boolean(),
27
+ hasPassword: z.boolean(),
28
+ isDeleted: z.boolean(),
29
+ ownedProducts: z.string().array(),
30
+ address: addressSchema.nullable(),
31
+ communicationPreferences: communicationPreferencesSchema
32
+ });
33
+
34
+ // src/licenses/schemas.ts
35
+ import { z as z2 } from "zod";
36
+
37
+ // src/licenses/models.ts
38
+ var LicenseStatus = /* @__PURE__ */ ((LicenseStatus2) => {
39
+ LicenseStatus2["Active"] = "Active";
40
+ LicenseStatus2["Revoked"] = "Revoked";
41
+ return LicenseStatus2;
42
+ })(LicenseStatus || {});
43
+ var ActivationStatus = /* @__PURE__ */ ((ActivationStatus2) => {
44
+ ActivationStatus2["Active"] = "Active";
45
+ ActivationStatus2["Revoked"] = "Revoked";
46
+ return ActivationStatus2;
47
+ })(ActivationStatus || {});
48
+ var ActivationMethod = /* @__PURE__ */ ((ActivationMethod2) => {
49
+ ActivationMethod2["Online"] = "Online";
50
+ ActivationMethod2["Offline"] = "Offline";
51
+ return ActivationMethod2;
52
+ })(ActivationMethod || {});
53
+
54
+ // src/licenses/schemas.ts
55
+ var licenseSchema = z2.object({
56
+ id: z2.string(),
57
+ status: z2.nativeEnum(LicenseStatus),
58
+ activeNumberOfActivations: z2.number(),
59
+ maxNumberOfActivations: z2.number()
60
+ });
61
+ var licenseActivationSchema = z2.object({
62
+ id: z2.string(),
63
+ name: z2.string(),
64
+ status: z2.nativeEnum(ActivationStatus),
65
+ activationMethod: z2.nativeEnum(ActivationMethod),
66
+ lastValidatedAt: z2.coerce.date(),
67
+ license: licenseSchema
68
+ });
69
+
70
+ // src/products/schemas.ts
71
+ import { z as z3 } from "zod";
72
+
73
+ // src/products/models.ts
74
+ var ProductStatus = /* @__PURE__ */ ((ProductStatus2) => {
75
+ ProductStatus2["Active"] = "Active";
76
+ ProductStatus2["Inactive"] = "Inactive";
77
+ return ProductStatus2;
78
+ })(ProductStatus || {});
79
+
80
+ // src/products/schemas.ts
81
+ var productSchema = z3.object({
82
+ id: z3.string(),
83
+ name: z3.string(),
84
+ tagline: z3.string(),
85
+ description: z3.string(),
86
+ website: z3.string().nullable(),
87
+ iconUrl: z3.string().nullable(),
88
+ status: z3.nativeEnum(ProductStatus),
89
+ purchasable: z3.boolean(),
90
+ currentReleaseVersion: z3.string().nullable()
91
+ });
92
+
93
+ // src/trials/schemas.ts
94
+ import { z as z4 } from "zod";
95
+
96
+ // src/trials/models.ts
97
+ var TrialStatus = /* @__PURE__ */ ((TrialStatus2) => {
98
+ TrialStatus2["Active"] = "Active";
99
+ TrialStatus2["Expired"] = "Expired";
100
+ return TrialStatus2;
101
+ })(TrialStatus || {});
102
+
103
+ // src/trials/schemas.ts
104
+ var trialSchema = z4.object({
105
+ id: z4.string(),
106
+ status: z4.nativeEnum(TrialStatus),
107
+ expiresAt: z4.coerce.date(),
108
+ lastValidatedAt: z4.coerce.date(),
109
+ ownerId: z4.string().optional()
110
+ });
111
+
112
+ // src/activationRequests/models.ts
113
+ var ActivationRequestStatus = /* @__PURE__ */ ((ActivationRequestStatus2) => {
114
+ ActivationRequestStatus2["Requested"] = "Requested";
115
+ ActivationRequestStatus2["Fulfilled"] = "Fulfilled";
116
+ ActivationRequestStatus2["Completed"] = "Completed";
117
+ return ActivationRequestStatus2;
118
+ })(ActivationRequestStatus || {});
119
+
120
+ // src/activationRequests/schemas.ts
121
+ var activationRequestSchema = z5.object({
122
+ id: z5.string(),
123
+ status: z5.nativeEnum(ActivationRequestStatus),
124
+ product: productSchema,
125
+ activation: licenseActivationSchema.optional(),
126
+ trial: trialSchema.optional(),
127
+ customer: customerSchema.optional()
128
+ });
129
+
130
+ // src/activationRequests/endpoints.ts
131
+ var ActivationRequestEndpoints = class {
132
+ constructor(api) {
133
+ this.api = api;
134
+ }
135
+ async get(requestId) {
136
+ const response = await this.api.fetch(`/api/activations/${requestId}`);
137
+ return activationRequestSchema.parse(response.data);
138
+ }
139
+ async request(productId) {
140
+ const response = await this.api.fetch(`/api/activations/${productId}/request`);
141
+ return activationRequestSchema.parse(response.data);
142
+ }
143
+ };
144
+
145
+ // src/customers/endpoints.ts
146
+ var CustomerEndpoints = class {
147
+ constructor(api) {
148
+ this.api = api;
149
+ }
150
+ async get(idOrEmail) {
151
+ const response = await this.api.fetch(`/api/customers/${idOrEmail}`);
152
+ return customerSchema.parse(response.data);
153
+ }
154
+ };
155
+
156
+ // src/licenses/endpoints.ts
157
+ var LicenseEndpoints = class {
158
+ constructor(api) {
159
+ this.api = api;
160
+ }
161
+ async validate(licenseId, activationId) {
162
+ const response = await this.api.fetch(`/api/licenses/${licenseId}/activations/${activationId}/validate`, "POST");
163
+ return licenseActivationSchema.parse(response.data);
164
+ }
165
+ };
166
+
167
+ // src/globalSchemas.ts
168
+ import { z as z6 } from "zod";
169
+ var priceCollectionSchema = z6.record(z6.number());
170
+ var percentageOffDiscountSchema = z6.object({
171
+ type: z6.literal("PercentageOffDiscount"),
172
+ name: z6.string(),
173
+ description: z6.string().optional(),
174
+ percentage: z6.number(),
175
+ total: priceCollectionSchema
176
+ });
177
+ var flatAmountOffDiscountSchema = z6.object({
178
+ type: z6.literal("FlatAmountOffDiscount"),
179
+ name: z6.string(),
180
+ description: z6.string().optional(),
181
+ total: priceCollectionSchema
182
+ });
183
+ var discountSchema = z6.discriminatedUnion("type", [
184
+ percentageOffDiscountSchema,
185
+ flatAmountOffDiscountSchema
186
+ ]);
187
+ var pricingVariationSchema = z6.object({
188
+ id: z6.string(),
189
+ name: z6.string(),
190
+ originalPrice: priceCollectionSchema,
191
+ price: priceCollectionSchema,
192
+ hasDiscount: z6.boolean(),
193
+ discount: discountSchema.optional()
194
+ });
195
+ function paged(itemSchema) {
196
+ return z6.object({
197
+ items: z6.array(itemSchema),
198
+ hasMore: z6.boolean(),
199
+ next: z6.string().nullable()
200
+ });
201
+ }
202
+
203
+ // src/utils/api.ts
204
+ import fetch from "cross-fetch";
205
+
206
+ // src/utils/problemHandler.ts
207
+ import { z as z7 } from "zod";
208
+
209
+ // src/utils/errors.ts
210
+ var NotAuthorizedError = class extends Error {
211
+ constructor() {
212
+ super();
213
+ this.name = "NotAuthorizedError";
214
+ }
215
+ };
216
+ var NotAuthenticatedError = class extends Error {
217
+ constructor() {
218
+ super();
219
+ this.name = "NotAuthenticatedError";
220
+ }
221
+ };
222
+ var NotFoundError = class extends Error {
223
+ constructor() {
224
+ super();
225
+ this.name = "NotFoundError";
226
+ }
227
+ };
228
+ var MoonbaseError = class extends Error {
229
+ constructor(title, detail, status) {
230
+ super();
231
+ this.title = title;
232
+ this.detail = detail;
233
+ this.status = status;
234
+ this.name = "MoonbaseError";
235
+ this.message = detail != null ? detail : title;
236
+ }
237
+ };
238
+
239
+ // src/utils/problemHandler.ts
240
+ var problemDetailsSchema = z7.object({
241
+ type: z7.string(),
242
+ title: z7.string(),
243
+ detail: z7.string().optional(),
244
+ status: z7.number()
245
+ });
246
+ async function handleResponseProblem(response) {
247
+ if (response.status === 404)
248
+ throw new NotFoundError();
249
+ if (response.status === 401)
250
+ throw new NotAuthenticatedError();
251
+ if (response.status === 403)
252
+ throw new NotAuthorizedError();
253
+ let problemDetails;
254
+ try {
255
+ const json = await response.json();
256
+ problemDetails = problemDetailsSchema.parse(json);
257
+ } catch (e) {
258
+ throw new Error("An unknown problem occurred");
259
+ }
260
+ throw new MoonbaseError(problemDetails.title, problemDetails.detail, problemDetails.status);
261
+ }
262
+
263
+ // src/utils/api.ts
264
+ function objectToQuery(obj) {
265
+ return Object.entries(obj != null ? obj : {}).filter(([_, value]) => value !== void 0).map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join("&");
266
+ }
267
+ var MoonbaseApi = class {
268
+ constructor(baseUrl, apiKey) {
269
+ this.baseUrl = baseUrl;
270
+ this.apiKey = apiKey;
271
+ }
272
+ async fetch(path, method, body, contentType) {
273
+ contentType != null ? contentType : contentType = "application/json";
274
+ const response = await fetch(this.baseUrl + path, {
275
+ method: method || "GET",
276
+ mode: "cors",
277
+ headers: {
278
+ "Accept": "application/json",
279
+ "Content-Type": contentType,
280
+ "Api-Key": this.apiKey
281
+ },
282
+ body: body ? contentType !== "application/json" ? body : JSON.stringify(body) : void 0
283
+ });
284
+ if (response.status >= 400)
285
+ await handleResponseProblem(response);
286
+ const contentLength = Number(response.headers.get("Content-Length")) || 0;
287
+ return {
288
+ data: contentLength > 0 ? await response.json() : null,
289
+ headers: response.headers,
290
+ status: response.status
291
+ };
292
+ }
293
+ };
294
+
295
+ // src/products/endpoints.ts
296
+ var ProductEndpoints = class {
297
+ constructor(api) {
298
+ this.api = api;
299
+ }
300
+ async query(opts) {
301
+ const response = await this.api.fetch(`/api/products?${objectToQuery(opts)}`);
302
+ return paged(productSchema).parse(response.data);
303
+ }
304
+ async get(id) {
305
+ const response = await this.api.fetch(`/api/products/${id}`);
306
+ return productSchema.parse(response.data);
307
+ }
308
+ };
309
+
310
+ // src/trials/endpoints.ts
311
+ var TrialEndpoints = class {
312
+ constructor(api) {
313
+ this.api = api;
314
+ }
315
+ async request(productId, target) {
316
+ const response = await this.api.fetch(`/api/trials/${productId}/request`, "POST", target);
317
+ return trialSchema.parse(response.data);
318
+ }
319
+ };
320
+
321
+ // src/index.ts
322
+ var MoonbaseClient = class {
323
+ constructor(configuration) {
324
+ this.configuration = configuration;
325
+ this.configuration.endpoint = this.configuration.endpoint.replace(/\/$/, "");
326
+ const api = new MoonbaseApi(this.configuration.endpoint, this.configuration.apiKey);
327
+ this.activationRequests = new ActivationRequestEndpoints(api);
328
+ this.customers = new CustomerEndpoints(api);
329
+ this.licenses = new LicenseEndpoints(api);
330
+ this.products = new ProductEndpoints(api);
331
+ this.trials = new TrialEndpoints(api);
332
+ }
333
+ };
334
+ export {
335
+ ActivationMethod,
336
+ ActivationRequestStatus,
337
+ ActivationStatus,
338
+ LicenseStatus,
339
+ MoonbaseClient,
340
+ MoonbaseError,
341
+ NotAuthenticatedError,
342
+ NotAuthorizedError,
343
+ NotFoundError,
344
+ ProductStatus,
345
+ TrialStatus
346
+ };
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@moonbase.sh/api",
3
+ "type": "module",
4
+ "version": "0.1.109",
5
+ "description": "Package to let you integrate backends with Moonbase.sh as payment and delivery provider",
6
+ "author": "Tobias Lønnerød Madsen <m@dsen.tv>",
7
+ "license": "MIT",
8
+ "sideEffects": false,
9
+ "main": "./dist/index.js",
10
+ "module": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "files": [
13
+ "dist/**"
14
+ ],
15
+ "dependencies": {
16
+ "cross-fetch": "^3.1.5",
17
+ "zod": "^3.21.4"
18
+ },
19
+ "devDependencies": {
20
+ "@types/node": "^18.14.2",
21
+ "rimraf": "^5.0.0",
22
+ "tsup": "^7.1.0",
23
+ "typescript": "~5.1.6"
24
+ },
25
+ "scripts": {
26
+ "build": "tsup src/index.ts --format esm,cjs --dts",
27
+ "dev": "tsup src/index.ts --format esm,cjs --watch --dts",
28
+ "version": "npm version"
29
+ }
30
+ }