bannerify-js 0.0.13 → 0.0.15

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.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { ChartData } from 'chart.js';
2
- export { HTTPError, TimeoutError } from 'ky';
3
2
 
4
3
  type Modification = {
5
4
  name: string;
@@ -75,26 +74,406 @@ type Modification = {
75
74
  visible?: boolean;
76
75
  };
77
76
 
77
+ /**
78
+ * This file was auto-generated by openapi-typescript.
79
+ * Do not make direct changes to the file.
80
+ */
81
+
82
+
83
+ interface paths {
84
+ "/v1/liveness": {
85
+ get: {
86
+ responses: {
87
+ /** @description The configured services and their status */
88
+ 200: {
89
+ content: {
90
+ "application/json": {
91
+ /** @description The status of the server */
92
+ status: string;
93
+ };
94
+ };
95
+ };
96
+ /** @description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing). */
97
+ 400: {
98
+ content: {
99
+ "application/json": components["schemas"]["ErrBadRequest"];
100
+ };
101
+ };
102
+ /** @description Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response. */
103
+ 401: {
104
+ content: {
105
+ "application/json": components["schemas"]["ErrUnauthorized"];
106
+ };
107
+ };
108
+ /** @description The client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource. Unlike 401 Unauthorized, the client's identity is known to the server. */
109
+ 403: {
110
+ content: {
111
+ "application/json": components["schemas"]["ErrForbidden"];
112
+ };
113
+ };
114
+ /** @description The server cannot find the requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 Forbidden to hide the existence of a resource from an unauthorized client. This response code is probably the most well known due to its frequent occurrence on the web. */
115
+ 404: {
116
+ content: {
117
+ "application/json": components["schemas"]["ErrNotFound"];
118
+ };
119
+ };
120
+ /** @description This response is sent when a request conflicts with the current state of the server. */
121
+ 409: {
122
+ content: {
123
+ "application/json": components["schemas"]["ErrConflict"];
124
+ };
125
+ };
126
+ /** @description The user has sent too many requests in a given amount of time ("rate limiting") */
127
+ 429: {
128
+ content: {
129
+ "application/json": components["schemas"]["ErrTooManyRequests"];
130
+ };
131
+ };
132
+ /** @description The server has encountered a situation it does not know how to handle. */
133
+ 500: {
134
+ content: {
135
+ "application/json": components["schemas"]["ErrInternalServerError"];
136
+ };
137
+ };
138
+ };
139
+ };
140
+ };
141
+ "/v1/templates/createImage": {
142
+ get: {
143
+ parameters: {
144
+ query: {
145
+ modifications?: string;
146
+ format?: "png" | "svg";
147
+ nocache?: string;
148
+ _debug?: string;
149
+ apiKey: string;
150
+ templateId: string;
151
+ };
152
+ };
153
+ responses: {
154
+ /** @description A image file */
155
+ 200: {
156
+ content: {
157
+ "image/png": unknown;
158
+ "image/svg+xml": string;
159
+ };
160
+ };
161
+ /** @description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing). */
162
+ 400: {
163
+ content: {
164
+ "application/json": components["schemas"]["ErrBadRequest"];
165
+ };
166
+ };
167
+ /** @description Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response. */
168
+ 401: {
169
+ content: {
170
+ "application/json": components["schemas"]["ErrUnauthorized"];
171
+ };
172
+ };
173
+ /** @description The client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource. Unlike 401 Unauthorized, the client's identity is known to the server. */
174
+ 403: {
175
+ content: {
176
+ "application/json": components["schemas"]["ErrForbidden"];
177
+ };
178
+ };
179
+ /** @description The server cannot find the requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 Forbidden to hide the existence of a resource from an unauthorized client. This response code is probably the most well known due to its frequent occurrence on the web. */
180
+ 404: {
181
+ content: {
182
+ "application/json": components["schemas"]["ErrNotFound"];
183
+ };
184
+ };
185
+ /** @description This response is sent when a request conflicts with the current state of the server. */
186
+ 409: {
187
+ content: {
188
+ "application/json": components["schemas"]["ErrConflict"];
189
+ };
190
+ };
191
+ /** @description The user has sent too many requests in a given amount of time ("rate limiting") */
192
+ 429: {
193
+ content: {
194
+ "application/json": components["schemas"]["ErrTooManyRequests"];
195
+ };
196
+ };
197
+ /** @description The server has encountered a situation it does not know how to handle. */
198
+ 500: {
199
+ content: {
200
+ "application/json": components["schemas"]["ErrInternalServerError"];
201
+ };
202
+ };
203
+ };
204
+ };
205
+ };
206
+ "/v1/templates/imageSignedUrl": {
207
+ get: {
208
+ parameters: {
209
+ query: {
210
+ modifications?: string;
211
+ format?: "png" | "svg";
212
+ nocache?: string;
213
+ _debug?: string;
214
+ templateId: string;
215
+ apiKeyMd5: string;
216
+ sign: string;
217
+ };
218
+ };
219
+ responses: {
220
+ /** @description A image file */
221
+ 200: {
222
+ content: {
223
+ "image/png": unknown;
224
+ };
225
+ };
226
+ /** @description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing). */
227
+ 400: {
228
+ content: {
229
+ "application/json": components["schemas"]["ErrBadRequest"];
230
+ };
231
+ };
232
+ /** @description Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response. */
233
+ 401: {
234
+ content: {
235
+ "application/json": components["schemas"]["ErrUnauthorized"];
236
+ };
237
+ };
238
+ /** @description The client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource. Unlike 401 Unauthorized, the client's identity is known to the server. */
239
+ 403: {
240
+ content: {
241
+ "application/json": components["schemas"]["ErrForbidden"];
242
+ };
243
+ };
244
+ /** @description The server cannot find the requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 Forbidden to hide the existence of a resource from an unauthorized client. This response code is probably the most well known due to its frequent occurrence on the web. */
245
+ 404: {
246
+ content: {
247
+ "application/json": components["schemas"]["ErrNotFound"];
248
+ };
249
+ };
250
+ /** @description This response is sent when a request conflicts with the current state of the server. */
251
+ 409: {
252
+ content: {
253
+ "application/json": components["schemas"]["ErrConflict"];
254
+ };
255
+ };
256
+ /** @description The user has sent too many requests in a given amount of time ("rate limiting") */
257
+ 429: {
258
+ content: {
259
+ "application/json": components["schemas"]["ErrTooManyRequests"];
260
+ };
261
+ };
262
+ /** @description The server has encountered a situation it does not know how to handle. */
263
+ 500: {
264
+ content: {
265
+ "application/json": components["schemas"]["ErrInternalServerError"];
266
+ };
267
+ };
268
+ };
269
+ };
270
+ };
271
+ }
272
+
273
+ interface components {
274
+ schemas: {
275
+ ErrBadRequest: {
276
+ error: {
277
+ /**
278
+ * @description A machine readable error code.
279
+ * @example BAD_REQUEST
280
+ * @enum {string}
281
+ */
282
+ code: "BAD_REQUEST";
283
+ /**
284
+ * @description A link to our documentation with more details about this error code
285
+ * @example https://bannerify.co/docs/api-reference/errors/code/BAD_REQUEST
286
+ */
287
+ docs: string;
288
+ /** @description A human readable explanation of what went wrong */
289
+ message: string;
290
+ /**
291
+ * @description Please always include the requestId in your error report
292
+ * @example req:1234
293
+ */
294
+ requestId: string;
295
+ };
296
+ };
297
+ ErrUnauthorized: {
298
+ error: {
299
+ /**
300
+ * @description A machine readable error code.
301
+ * @example UNAUTHORIZED
302
+ * @enum {string}
303
+ */
304
+ code: "UNAUTHORIZED";
305
+ /**
306
+ * @description A link to our documentation with more details about this error code
307
+ * @example https://bannerify.co/docs/api-reference/errors/code/UNAUTHORIZED
308
+ */
309
+ docs: string;
310
+ /** @description A human readable explanation of what went wrong */
311
+ message: string;
312
+ /**
313
+ * @description Please always include the requestId in your error report
314
+ * @example req:1234
315
+ */
316
+ requestId: string;
317
+ };
318
+ };
319
+ ErrForbidden: {
320
+ error: {
321
+ /**
322
+ * @description A machine readable error code.
323
+ * @example FORBIDDEN
324
+ * @enum {string}
325
+ */
326
+ code: "FORBIDDEN";
327
+ /**
328
+ * @description A link to our documentation with more details about this error code
329
+ * @example https://bannerify.co/docs/api-reference/errors/code/FORBIDDEN
330
+ */
331
+ docs: string;
332
+ /** @description A human readable explanation of what went wrong */
333
+ message: string;
334
+ /**
335
+ * @description Please always include the requestId in your error report
336
+ * @example req:1234
337
+ */
338
+ requestId: string;
339
+ };
340
+ };
341
+ ErrNotFound: {
342
+ error: {
343
+ /**
344
+ * @description A machine readable error code.
345
+ * @example NOT_FOUND
346
+ * @enum {string}
347
+ */
348
+ code: "NOT_FOUND";
349
+ /**
350
+ * @description A link to our documentation with more details about this error code
351
+ * @example https://bannerify.co/docs/api-reference/errors/code/NOT_FOUND
352
+ */
353
+ docs: string;
354
+ /** @description A human readable explanation of what went wrong */
355
+ message: string;
356
+ /**
357
+ * @description Please always include the requestId in your error report
358
+ * @example req:1234
359
+ */
360
+ requestId: string;
361
+ };
362
+ };
363
+ ErrConflict: {
364
+ error: {
365
+ /**
366
+ * @description A machine readable error code.
367
+ * @example CONFLICT
368
+ * @enum {string}
369
+ */
370
+ code: "CONFLICT";
371
+ /**
372
+ * @description A link to our documentation with more details about this error code
373
+ * @example https://bannerify.co/docs/api-reference/errors/code/CONFLICT
374
+ */
375
+ docs: string;
376
+ /** @description A human readable explanation of what went wrong */
377
+ message: string;
378
+ /**
379
+ * @description Please always include the requestId in your error report
380
+ * @example req:1234
381
+ */
382
+ requestId: string;
383
+ };
384
+ };
385
+ ErrTooManyRequests: {
386
+ error: {
387
+ /**
388
+ * @description A machine readable error code.
389
+ * @example TOO_MANY_REQUESTS
390
+ * @enum {string}
391
+ */
392
+ code: "TOO_MANY_REQUESTS";
393
+ /**
394
+ * @description A link to our documentation with more details about this error code
395
+ * @example https://bannerify.co/docs/api-reference/errors/code/TOO_MANY_REQUESTS
396
+ */
397
+ docs: string;
398
+ /** @description A human readable explanation of what went wrong */
399
+ message: string;
400
+ /**
401
+ * @description Please always include the requestId in your error report
402
+ * @example req:1234
403
+ */
404
+ requestId: string;
405
+ };
406
+ };
407
+ ErrInternalServerError: {
408
+ error: {
409
+ /**
410
+ * @description A machine readable error code.
411
+ * @example INTERNAL_SERVER_ERROR
412
+ * @enum {string}
413
+ */
414
+ code: "INTERNAL_SERVER_ERROR";
415
+ /**
416
+ * @description A link to our documentation with more details about this error code
417
+ * @example https://bannerify.co/docs/api-reference/errors/code/INTERNAL_SERVER_ERROR
418
+ */
419
+ docs: string;
420
+ /** @description A human readable explanation of what went wrong */
421
+ message: string;
422
+ /**
423
+ * @description Please always include the requestId in your error report
424
+ * @example req:1234
425
+ */
426
+ requestId: string;
427
+ };
428
+ };
429
+ };
430
+ responses: never;
431
+ parameters: {
432
+ };
433
+ requestBodies: never;
434
+ headers: never;
435
+ pathItems: never;
436
+ }
437
+
438
+ type codes = '400' | '401' | '403' | '404' | '500';
439
+ type ErrorResponse = paths['/v1/liveness']['get']['responses'][codes]['content']['application/json'];
440
+ declare const timeoutError: {
441
+ error: {
442
+ code: string;
443
+ message: string;
444
+ docs: string;
445
+ requestId: string;
446
+ };
447
+ };
448
+ type Result<R> = {
449
+ result: R;
450
+ error?: never;
451
+ } | {
452
+ result?: never;
453
+ error: ErrorResponse['error'] | typeof timeoutError;
454
+ };
455
+
78
456
  interface Options {
79
457
  apiKey: string;
80
458
  fetch?: typeof fetch;
81
459
  baseUrl?: string;
460
+ timeout?: number;
82
461
  }
83
462
  type CreateOptions = {
84
463
  modifications?: Modification[];
85
464
  container?: Modification;
86
465
  format?: 'svg' | 'png';
87
- timeout?: number;
88
466
  };
89
- declare function createClient(opts: Options): Bannerify;
467
+ declare function createClient(apiKey: string, opts?: Options): Bannerify;
90
468
  declare class Bannerify {
91
469
  #private;
92
- private readonly opts;
470
+ private readonly apiKey;
471
+ private readonly opts?;
93
472
  private readonly client;
94
473
  private readonly baseUrl;
95
- constructor(opts: Options);
96
- createImage(templateId: string, options?: CreateOptions): Promise<string | ArrayBuffer>;
97
- createPdf(templateId: string, options?: CreateOptions): Promise<ArrayBuffer>;
474
+ constructor(apiKey: string, opts?: Options | undefined);
475
+ createImage(templateId: string, options?: CreateOptions): Promise<Result<ArrayBuffer | string>>;
476
+ createPdf(templateId: string, options?: CreateOptions): Promise<void>;
98
477
  createPermanentImage(): Promise<void>;
99
478
  generateImageSignedUrl(templateId: string, options?: CreateOptions): Promise<string>;
100
479
  }
package/dist/index.js CHANGED
@@ -525,52 +525,94 @@ var ky = createInstance();
525
525
  var distribution_default = ky;
526
526
 
527
527
  // package.json
528
- var version = "0.0.12";
528
+ var version = "0.0.14";
529
+
530
+ // src/telemetry.ts
531
+ function getTelemetry() {
532
+ let platform;
533
+ let runtime;
534
+ const sdkVersions = [`bannerify-js@${version}`];
535
+ try {
536
+ if (process?.env?.UNKEY_DISABLE_TELEMETRY) {
537
+ return null;
538
+ }
539
+ platform = process.env.VERCEL ? "vercel" : process.env.AWS_REGION ? "aws" : void 0;
540
+ if (typeof EdgeRuntime !== "undefined") {
541
+ runtime = "edge-light";
542
+ } else if (typeof caches !== "undefined") {
543
+ runtime = "cloudflare";
544
+ } else if (process?.version) {
545
+ runtime = `node@${process.version}`;
546
+ } else {
547
+ runtime = "fetch-compatible";
548
+ }
549
+ } catch (_error) {
550
+ }
551
+ return { platform, runtime, sdkVersions };
552
+ }
553
+
554
+ // src/types.ts
555
+ var timeoutError = {
556
+ error: {
557
+ // @ts-ignore
558
+ code: "TIMEOUT_ERROR",
559
+ message: "No response",
560
+ docs: "",
561
+ requestId: "N/A"
562
+ }
563
+ };
529
564
 
530
565
  // src/client.ts
531
- function createClient(opts) {
532
- return new Bannerify(opts);
566
+ function createClient(apiKey, opts) {
567
+ return new Bannerify(apiKey, opts);
533
568
  }
534
569
  var Bannerify = class {
535
- constructor(opts) {
570
+ constructor(apiKey, opts) {
571
+ this.apiKey = apiKey;
536
572
  this.opts = opts;
537
- this.baseUrl = opts.baseUrl || "https://api-beta.bannerify.co/api/v1";
573
+ const telemetry = getTelemetry();
574
+ this.baseUrl = opts?.baseUrl || "https://api-beta.bannerify.co/v1";
538
575
  this.client = distribution_default.create({
539
- fetch: opts.fetch || fetch,
576
+ fetch: opts?.fetch || fetch,
540
577
  prefixUrl: this.baseUrl,
541
578
  headers: {
542
- Authorization: `Bearer ${opts.apiKey}`,
543
- "X-SDK-Version": `${version}`
544
- }
579
+ Authorization: `Bearer ${apiKey}`,
580
+ "X-SDK-Version": `${telemetry?.sdkVersions}`,
581
+ "X-Platform": telemetry?.platform,
582
+ "X-Runtime": telemetry?.runtime
583
+ },
584
+ timeout: opts?.timeout ?? 1e4
545
585
  });
546
586
  }
547
587
  client;
548
588
  baseUrl;
549
589
  async createImage(templateId, options) {
550
- const res = this.client.get("templates/createImage", {
551
- searchParams: {
552
- modifications: JSON.stringify(options?.modifications ?? []),
553
- container: JSON.stringify(options?.container ?? {}),
554
- templateId,
555
- apiKey: this.opts.apiKey,
556
- format: options?.format
557
- },
558
- timeout: options?.timeout
559
- });
560
- if (options?.format === "svg") {
561
- return res.text();
590
+ try {
591
+ const res = this.client.get("templates/createImage", {
592
+ json: {
593
+ modifications: options?.modifications ?? [],
594
+ container: JSON.stringify(options?.container ?? {}),
595
+ templateId,
596
+ apiKey: this.apiKey,
597
+ format: options?.format
598
+ }
599
+ });
600
+ if (options?.format === "svg") {
601
+ return { result: await res.text() };
602
+ }
603
+ return { result: await res.arrayBuffer() };
604
+ } catch (e) {
605
+ if (e instanceof HTTPError) {
606
+ return await e.response.json();
607
+ }
608
+ if (e instanceof TimeoutError) {
609
+ return timeoutError;
610
+ }
611
+ throw e;
562
612
  }
563
- return res.arrayBuffer();
564
613
  }
565
614
  async createPdf(templateId, options) {
566
- return this.client.get("templates/createPdf", {
567
- searchParams: {
568
- modifications: JSON.stringify(options?.modifications ?? []),
569
- templateId,
570
- apiKey: this.opts.apiKey
571
- },
572
- timeout: options?.timeout
573
- }).arrayBuffer();
615
+ throw new Error("Not yet implemented");
574
616
  }
575
617
  async createPermanentImage() {
576
618
  throw new Error("Not yet implemented");
@@ -583,7 +625,7 @@ var Bannerify = class {
583
625
  return hashHex;
584
626
  };
585
627
  async generateImageSignedUrl(templateId, options) {
586
- const apiKeyAsMd5 = await this.#hashText(this.opts.apiKey);
628
+ const apiKeyAsMd5 = await this.#hashText(this.apiKey);
587
629
  const searchParams = new URLSearchParams();
588
630
  searchParams.set("apiKeyMd5", apiKeyAsMd5);
589
631
  if (options?.format === "svg") {
@@ -595,13 +637,11 @@ var Bannerify = class {
595
637
  searchParams.set("templateId", templateId);
596
638
  searchParams.sort();
597
639
  searchParams.set("sign", await this.#hashText(searchParams.toString() + searchParams.get("apiKeyMd5")));
598
- return `${this.baseUrl}/templates/imageSignedUrl?${searchParams.toString()}`;
640
+ return `${this.baseUrl}/templates/signedurl?${searchParams.toString()}`;
599
641
  }
600
642
  };
601
643
  export {
602
644
  Bannerify,
603
- HTTPError,
604
- TimeoutError,
605
645
  createClient
606
646
  };
607
647
  /*! Bundled license information: