@hyphen/sdk 1.9.0 → 1.10.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/README.md CHANGED
@@ -12,7 +12,7 @@ The Hyphen Node.js SDK is a JavaScript library that allows developers to easily
12
12
  # Table of Contents
13
13
  - [Installation](#installation)
14
14
  - [Basic Usage with Hyphen](#basic-usage-with-hyphen)
15
- - [Toggle](#toggle)
15
+ - [Toggle - Feature Flag Service](#toggle---feature-flag-service)
16
16
  - [Toggle Options](#toggle-options)
17
17
  - [Toggle API](#toggle-api)
18
18
  - [Toggle Hooks](#toggle-hooks)
@@ -20,9 +20,10 @@ The Hyphen Node.js SDK is a JavaScript library that allows developers to easily
20
20
  - [Toggle Caching](#toggle-caching)
21
21
  - [Toggle Environment Variables](#toggle-environment-variables)
22
22
  - [Toggle Self-Hosted](#toggle-self-hosted)
23
- - [ENV](#env)
23
+ - [ENV - Secret Management Service](#env---secret-management-service)
24
24
  - [Loading Environment Variables](#loading-environment-variables)
25
- - [Net Info](#net-info)
25
+ - [Net Info - Geo Information Service](#net-info---geo-information-service)
26
+ - [Link - Short Code Service](#link---short-code-service)
26
27
  - [Contributing](#contributing)
27
28
  - [Testing Your Changes](#testing-your-changes)
28
29
  - [License and Copyright](#license-and-copyright)
@@ -123,7 +124,7 @@ console.log('Geo IP information:', result);
123
124
 
124
125
  The rest of the examples for each service show you accessing the service instance directly.
125
126
 
126
- # Toggle
127
+ # Toggle - Feature Flag Service
127
128
 
128
129
  [Toggle](https://hyphen.ai/toggle) is our feature flag service that allows you to control the rollout of new features to your users. You can access your feature flags using the `Toggle` class.
129
130
 
@@ -571,7 +572,7 @@ const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
571
572
  console.log('Boolean toggle value:', result); // true
572
573
  ```
573
574
 
574
- # ENV
575
+ # ENV - Secret Management Service
575
576
 
576
577
  Hyphens secret management service known as [ENV](https://hyphen.ai/env) allows you to manage your environment variables in a secure way. The Hyphen Node.js SDK provides a simple way to access your environment variables.
577
578
 
@@ -616,7 +617,7 @@ import { loadEnv } from '@hyphen/sdk';
616
617
  loadEnv({ local: false });
617
618
  ```
618
619
 
619
- # Net Info
620
+ # Net Info - Geo Information Service
620
621
 
621
622
  The Hyphen Node.js SDK also provides a `NetInfo` class that allows you to fetch geo information about an IP address. This can be useful for debugging or logging purposes. You can read more about it:
622
623
 
@@ -650,6 +651,38 @@ console.log('IP Infos:', ipInfos);
650
651
 
651
652
  You can also set the API key using the `HYPHEN_API_KEY` environment variable. This is useful for keeping your API key secure and not hardcoding it in your code.
652
653
 
654
+ # Link - Short Code Service
655
+
656
+ The Hyphen Node.js SDK also provides a `Link` class that allows you to create and manage short codes. This can be useful for generating short links for your application. Here is an example of creating a short code:
657
+
658
+ ```javascript
659
+ import { Link } from '@hyphen/sdk';
660
+ const link = new Link({
661
+ organizationId: 'your_organization_id',
662
+ apiKey: 'your_api_key',
663
+ });
664
+ const longUrl = 'https://hyphen.ai';
665
+ const domain = 'test.h4n.link';
666
+ const options = {
667
+ tags: ['sdk-test', 'unit-test'],
668
+ };
669
+ const response = await link.createShortCode(longUrl, domain, options);
670
+ console.log('Short Code Response:', response);
671
+ ```
672
+
673
+ if you want to delete a short code you can do it like this:
674
+
675
+ ```javascript
676
+ import { Link } from '@hyphen/sdk';
677
+ const link = new Link({
678
+ organizationId: 'your_organization_id',
679
+ apiKey: 'your_api_key',
680
+ });
681
+ const code = 'code_1234567890'; // It is the code identifier for the short code you want to delete
682
+ const response = await link.deleteShortCode(code);
683
+ console.log('Delete Short Code Response:', response);
684
+ ```
685
+
653
686
  # Contributing
654
687
 
655
688
  We welcome contributions to the Hyphen Node.js SDK! If you have an idea for a new feature, bug fix, or improvement, please follow these steps:
@@ -690,9 +723,19 @@ Once you have created the project, added the toggles, and created your applicati
690
723
  HYPHEN_PUBLIC_API_KEY=your_public_api_key
691
724
  HYPHEN_API_KEY=your_api_key
692
725
  HYPHEN_APPLICATION_ID=your_project_id
726
+ HYPHEN_LINK_DOMAIN=your_link_domain
727
+ HYPHEN_ORGANIZATION_ID=your_organization_id
693
728
  ```
694
729
 
695
- The `HYPHEN_PUBLIC_API_KEY` is the public API key for your Hyphen project, `HYPHEN_API_KEY` is the API key used for things such as `NetInfo` and is located under settings in the dashboard, and `HYPHEN_APPLICATION_ID` is the application ID for your Hyphen project.
730
+ A bit more information about the environment variables:
731
+
732
+ | Variable | Example Value | Description |
733
+ |----------------|----------------|----------------|
734
+ | *HYPHEN_PUBLIC_API_KEY* | `public_api_key` | The public API key for your Hyphen project. You can find this in the Hyphen dashboard. |
735
+ | *HYPHEN_API_KEY* | `api_key` | The API key for your Hyphen project. You can find this in the Hyphen dashboard. |
736
+ | *HYPHEN_APPLICATION_ID* | `application_id` | The application ID for your Hyphen project. You can find this in the Hyphen dashboard. |
737
+ | *HYPHEN_LINK_DOMAIN* | `test.h4n.link` | The domain for the Link service. This is used for generating links. |
738
+ | *HYPHEN_ORGANIZATION_ID* | `org_668575c0e169cde974a5c76a` | | The organization ID for your Hyphen project. This is used for the Link service. |
696
739
 
697
740
  Then run the tests with the following command:
698
741
 
package/dist/index.cjs CHANGED
@@ -661,6 +661,133 @@ var NetInfo = class extends BaseService {
661
661
  }
662
662
  };
663
663
 
664
+ // src/link.ts
665
+ var import_node_process4 = __toESM(require("process"), 1);
666
+ loadEnv();
667
+ var defaultLinkUris = [
668
+ "https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"
669
+ ];
670
+ var Link = class extends BaseService {
671
+ static {
672
+ __name(this, "Link");
673
+ }
674
+ _uris = defaultLinkUris;
675
+ _organizationId;
676
+ _apiKey;
677
+ constructor(options) {
678
+ super(options);
679
+ this._uris = options?.uris ?? defaultLinkUris;
680
+ this._organizationId = options?.organizationId;
681
+ if (options?.apiKey) {
682
+ this.setApiKey(options.apiKey);
683
+ }
684
+ if (!this._apiKey && import_node_process4.default.env.HYPHEN_API_KEY) {
685
+ this.setApiKey(import_node_process4.default.env.HYPHEN_API_KEY);
686
+ }
687
+ if (!this._organizationId && import_node_process4.default.env.HYPHEN_ORGANIZATION_ID) {
688
+ this._organizationId = import_node_process4.default.env.HYPHEN_ORGANIZATION_ID;
689
+ }
690
+ }
691
+ /**
692
+ * Get the URIs for the link service. The default is `["https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"]`.
693
+ * @returns {string[]} The URIs for the link service.
694
+ */
695
+ get uris() {
696
+ return this._uris;
697
+ }
698
+ /**
699
+ * Set the URIs for the link service. The default is `["https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"]`.
700
+ * @param {string[]} uris - The URIs to set.
701
+ */
702
+ set uris(uris) {
703
+ this._uris = uris;
704
+ }
705
+ /**
706
+ * Get the organization ID for the link service. This is required to access the link service.
707
+ * @returns {string | undefined} The organization ID.
708
+ */
709
+ get organizationId() {
710
+ return this._organizationId;
711
+ }
712
+ /**
713
+ * Set the organization ID for the link service. This is required to access the link service.
714
+ * @param {string | undefined} organizationId - The organization ID to set.
715
+ */
716
+ set organizationId(organizationId) {
717
+ this._organizationId = organizationId;
718
+ }
719
+ /**
720
+ * Get the API key for the link service. This is required to access the link service.
721
+ * @returns {string | undefined} The API key.
722
+ */
723
+ get apiKey() {
724
+ return this._apiKey;
725
+ }
726
+ /**
727
+ * Set the API key for the link service. This is required to access the link service.
728
+ * @param {string | undefined} apiKey - The API key to set.
729
+ */
730
+ set apiKey(apiKey) {
731
+ this.setApiKey(apiKey);
732
+ }
733
+ /**
734
+ * Set the API key for the link service. If the API key starts with 'public_', an error is thrown.
735
+ * This is to ensure that the API key is not a public key, which should not be used for authenticated requests.
736
+ * @param {string} apiKey
737
+ */
738
+ setApiKey(apiKey) {
739
+ if (apiKey?.startsWith("public_")) {
740
+ throw new Error('API key cannot start with "public_"');
741
+ }
742
+ if (apiKey) {
743
+ this._apiKey = apiKey;
744
+ }
745
+ }
746
+ async createShortCode(longUrl, domain, options) {
747
+ if (!this._organizationId) {
748
+ throw new Error("Organization ID is required to create a short code.");
749
+ }
750
+ const url = this._uris[0].replace("{organizationId}", this._organizationId);
751
+ const body = {
752
+ // eslint-disable-next-line @typescript-eslint/naming-convention
753
+ long_url: longUrl,
754
+ domain,
755
+ code: options?.code,
756
+ title: options?.title,
757
+ tags: options?.tags
758
+ };
759
+ const headers = this.createHeaders(this._apiKey);
760
+ const response = await this.post(url, body, {
761
+ headers
762
+ });
763
+ if (response.status === 201) {
764
+ return response.data;
765
+ }
766
+ throw new Error(`Failed to create short code: ${response.statusText}`);
767
+ }
768
+ /**
769
+ * Delete a short code.
770
+ * @param {string} code The short code to delete. Example: 'code_686bed403c3991bd676bba4d'
771
+ * @returns {Promise<boolean>} A promise that resolves to true if the short code was deleted successfully, or false if it was not.
772
+ */
773
+ async deleteShortCode(code) {
774
+ if (!this._organizationId) {
775
+ throw new Error("Organization ID is required to delete a short code.");
776
+ }
777
+ let url = this._uris[0].replace("{organizationId}", this._organizationId);
778
+ url = url.endsWith("/") ? `${url}${code}/` : `${url}/${code}/`;
779
+ const headers = this.createHeaders(this._apiKey);
780
+ delete headers["content-type"];
781
+ const response = await this.delete(url, {
782
+ headers
783
+ });
784
+ if (response.status === 204) {
785
+ return true;
786
+ }
787
+ throw new Error(`Failed to delete short code: ${response.statusText}`);
788
+ }
789
+ };
790
+
664
791
  // src/hyphen.ts
665
792
  var Hyphen = class extends import_hookified3.Hookified {
666
793
  static {
@@ -668,12 +795,14 @@ var Hyphen = class extends import_hookified3.Hookified {
668
795
  }
669
796
  _netInfo;
670
797
  _toggle;
798
+ _link;
671
799
  _publicApiKey;
672
800
  _apiKey;
673
801
  constructor(options) {
674
802
  super(options);
675
803
  const toggleOptions = options?.toggle ?? {};
676
804
  const netInfoOptions = options?.netInfo ?? {};
805
+ const linkOptions = options?.link ?? {};
677
806
  if (options?.publicApiKey) {
678
807
  this._publicApiKey = options.publicApiKey;
679
808
  toggleOptions.publicApiKey = options.publicApiKey;
@@ -681,10 +810,12 @@ var Hyphen = class extends import_hookified3.Hookified {
681
810
  if (options?.apiKey) {
682
811
  this._apiKey = options.apiKey;
683
812
  netInfoOptions.apiKey = options.apiKey;
813
+ linkOptions.apiKey = options.apiKey;
684
814
  }
685
815
  if (options?.throwErrors !== void 0) {
686
816
  toggleOptions.throwErrors = options.throwErrors;
687
817
  netInfoOptions.throwErrors = options.throwErrors;
818
+ linkOptions.throwErrors = options.throwErrors;
688
819
  }
689
820
  this._netInfo = new NetInfo(netInfoOptions);
690
821
  this._netInfo.on("error", (message, ...args) => this.emit("error", message, ...args));
@@ -694,33 +825,84 @@ var Hyphen = class extends import_hookified3.Hookified {
694
825
  this._toggle.on("error", (message, ...args) => this.emit("error", message, ...args));
695
826
  this._toggle.on("info", (message, ...args) => this.emit("info", message, ...args));
696
827
  this._toggle.on("warn", (message, ...args) => this.emit("warn", message, ...args));
828
+ this._link = new Link(linkOptions);
829
+ this._link.on("error", (message, ...args) => this.emit("error", message, ...args));
830
+ this._link.on("info", (message, ...args) => this.emit("info", message, ...args));
831
+ this._link.on("warn", (message, ...args) => this.emit("warn", message, ...args));
697
832
  }
833
+ /**
834
+ * Get the NetInfo service instance.
835
+ * @returns {NetInfo} The NetInfo service instance.
836
+ */
698
837
  get netInfo() {
699
838
  return this._netInfo;
700
839
  }
840
+ /**
841
+ * Get the Toggle service instance.
842
+ * @returns {Toggle} The Toggle service instance.
843
+ */
701
844
  get toggle() {
702
845
  return this._toggle;
703
846
  }
847
+ /**
848
+ * Get the Link service instance.
849
+ * @returns {Link} The Link service instance.
850
+ */
851
+ get link() {
852
+ return this._link;
853
+ }
854
+ /**
855
+ * Get the public API key for the Hyphen service.
856
+ * This is used for public endpoints that do not require authentication.
857
+ * @returns {string | undefined} The public API key.
858
+ */
704
859
  get publicApiKey() {
705
860
  return this._publicApiKey;
706
861
  }
862
+ /**
863
+ * Set the public API key for the Hyphen service. If set, this will also update the underlying services.
864
+ * This is used for public endpoints that do not require authentication such as the Toggle service.
865
+ * @param {string | undefined} value - The public API key to set.
866
+ */
707
867
  set publicApiKey(value) {
708
868
  this._publicApiKey = value;
709
869
  this._toggle.publicApiKey = value;
710
870
  }
871
+ /**
872
+ * Get the API key for the Hyphen service.
873
+ * This is used for authenticated endpoints that require an API key such as the NetInfo and Link services.
874
+ * @returns {string | undefined} The API key.
875
+ */
711
876
  get apiKey() {
712
877
  return this._apiKey;
713
878
  }
879
+ /**
880
+ * Set the API key for the Hyphen service. If set, this will also update the underlying services.
881
+ * This is used for authenticated endpoints that require an API key such as the NetInfo and Link services.
882
+ * @param {string | undefined} value - The API key to set.
883
+ */
714
884
  set apiKey(value) {
715
885
  this._apiKey = value;
716
886
  this._netInfo.apiKey = value;
887
+ this._link.apiKey = value;
717
888
  }
889
+ /**
890
+ * Get whether to throw errors or not.
891
+ * If set to true, errors will be thrown instead of logged.
892
+ * @returns {boolean} Whether to throw errors or not.
893
+ */
718
894
  get throwErrors() {
719
- return this._netInfo.throwErrors && this._toggle.throwErrors;
895
+ return this._netInfo.throwErrors && this._toggle.throwErrors && this._link.throwErrors;
720
896
  }
897
+ /**
898
+ * Set whether to throw errors or not. If set to true, errors will be thrown instead of logged.
899
+ * This will update the underlying services as well.
900
+ * @param {boolean} value - Whether to throw errors or not.
901
+ */
721
902
  set throwErrors(value) {
722
903
  this._netInfo.throwErrors = value;
723
904
  this._toggle.throwErrors = value;
905
+ this._link.throwErrors = value;
724
906
  }
725
907
  };
726
908
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.d.cts CHANGED
@@ -315,26 +315,198 @@ declare class NetInfo extends BaseService {
315
315
  getIpInfos(ips: string[]): Promise<Array<ipInfo | ipInfoError>>;
316
316
  }
317
317
 
318
+ type CreateShortCodeOptions = {
319
+ /**
320
+ * The short code used for this link. If not provided, a random code will be generated.
321
+ * @default undefined
322
+ */
323
+ code?: string;
324
+ /**
325
+ * The title of the link. This is used for display purposes.
326
+ * @default undefined
327
+ */
328
+ title?: string;
329
+ /**
330
+ * The tags associated with the link. This is used for categorization purposes.
331
+ * @default undefined
332
+ */
333
+ tags?: string[];
334
+ };
335
+ type CreateShortCodeResponse = {
336
+ id: string;
337
+ code: string;
338
+ long_url: string;
339
+ domain: string;
340
+ createdAt: string;
341
+ title?: string;
342
+ tags?: string[];
343
+ organizationId: {
344
+ id: string;
345
+ name: string;
346
+ };
347
+ };
348
+ type LinkOptions = {
349
+ /**
350
+ * The URIs to access the link service.
351
+ * @default ["https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"]
352
+ */
353
+ uris?: string[];
354
+ /**
355
+ * The organization ID to use for the link service.
356
+ * @requires organizationId
357
+ */
358
+ organizationId?: string;
359
+ /**
360
+ * The API key to use for the link service. This should be provided as the service requires authentication.
361
+ */
362
+ apiKey?: string;
363
+ } & BaseServiceOptions;
364
+ declare class Link extends BaseService {
365
+ private _uris;
366
+ private _organizationId?;
367
+ private _apiKey?;
368
+ constructor(options?: LinkOptions);
369
+ /**
370
+ * Get the URIs for the link service. The default is `["https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"]`.
371
+ * @returns {string[]} The URIs for the link service.
372
+ */
373
+ get uris(): string[];
374
+ /**
375
+ * Set the URIs for the link service. The default is `["https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"]`.
376
+ * @param {string[]} uris - The URIs to set.
377
+ */
378
+ set uris(uris: string[]);
379
+ /**
380
+ * Get the organization ID for the link service. This is required to access the link service.
381
+ * @returns {string | undefined} The organization ID.
382
+ */
383
+ get organizationId(): string | undefined;
384
+ /**
385
+ * Set the organization ID for the link service. This is required to access the link service.
386
+ * @param {string | undefined} organizationId - The organization ID to set.
387
+ */
388
+ set organizationId(organizationId: string | undefined);
389
+ /**
390
+ * Get the API key for the link service. This is required to access the link service.
391
+ * @returns {string | undefined} The API key.
392
+ */
393
+ get apiKey(): string | undefined;
394
+ /**
395
+ * Set the API key for the link service. This is required to access the link service.
396
+ * @param {string | undefined} apiKey - The API key to set.
397
+ */
398
+ set apiKey(apiKey: string | undefined);
399
+ /**
400
+ * Set the API key for the link service. If the API key starts with 'public_', an error is thrown.
401
+ * This is to ensure that the API key is not a public key, which should not be used for authenticated requests.
402
+ * @param {string} apiKey
403
+ */
404
+ setApiKey(apiKey: string | undefined): void;
405
+ createShortCode(longUrl: string, domain: string, options?: CreateShortCodeOptions): Promise<CreateShortCodeResponse>;
406
+ /**
407
+ * Delete a short code.
408
+ * @param {string} code The short code to delete. Example: 'code_686bed403c3991bd676bba4d'
409
+ * @returns {Promise<boolean>} A promise that resolves to true if the short code was deleted successfully, or false if it was not.
410
+ */
411
+ deleteShortCode(code: string): Promise<boolean>;
412
+ }
413
+
318
414
  type HyphenOptions = {
415
+ /**
416
+ * The public API key to use for the Hyphen service.
417
+ * This is used for public endpoints that do not require authentication.
418
+ */
319
419
  publicApiKey?: string;
420
+ /**
421
+ * The API key to use for the Hyphen service.
422
+ * This is used for authenticated endpoints that require an API key.
423
+ */
320
424
  apiKey?: string;
425
+ /**
426
+ * Whether to throw errors or not.
427
+ * If set to true, errors will be thrown instead of logged.
428
+ * @default false
429
+ */
321
430
  throwErrors?: boolean;
322
- toggle?: Omit<ToggleOptions, 'apiKey' | 'publicApiKey' | 'throwErrors'>;
323
- netInfo?: Omit<NetInfoOptions, 'apiKey' | 'publicApiKey' | 'throwErrors'>;
431
+ /**
432
+ * Options for the Toggle service.
433
+ * Excludes publicApiKey and throwErrors from ToggleOptions.
434
+ * @see ToggleOptions
435
+ * @default {Toggle}
436
+ */
437
+ toggle?: Omit<ToggleOptions, 'publicApiKey' | 'throwErrors'>;
438
+ /**
439
+ * Options for the NetInfo service.
440
+ * Excludes apiKey and throwErrors from NetInfoOptions.
441
+ * @see NetInfoOptions
442
+ * @default {NetInfo}
443
+ */
444
+ netInfo?: Omit<NetInfoOptions, 'apiKey' | 'throwErrors'>;
445
+ /**
446
+ * Options for the Link service.
447
+ * Excludes apiKey and throwErrors from LinkOptions.
448
+ * @see LinkOptions
449
+ * @default {Link}
450
+ */
451
+ link?: Omit<LinkOptions, 'apiKey' | 'throwErrors'>;
324
452
  } & HookifiedOptions;
325
453
  declare class Hyphen extends Hookified {
326
454
  private readonly _netInfo;
327
455
  private readonly _toggle;
456
+ private readonly _link;
328
457
  private _publicApiKey?;
329
458
  private _apiKey?;
330
459
  constructor(options?: HyphenOptions);
460
+ /**
461
+ * Get the NetInfo service instance.
462
+ * @returns {NetInfo} The NetInfo service instance.
463
+ */
331
464
  get netInfo(): NetInfo;
465
+ /**
466
+ * Get the Toggle service instance.
467
+ * @returns {Toggle} The Toggle service instance.
468
+ */
332
469
  get toggle(): Toggle;
470
+ /**
471
+ * Get the Link service instance.
472
+ * @returns {Link} The Link service instance.
473
+ */
474
+ get link(): Link;
475
+ /**
476
+ * Get the public API key for the Hyphen service.
477
+ * This is used for public endpoints that do not require authentication.
478
+ * @returns {string | undefined} The public API key.
479
+ */
333
480
  get publicApiKey(): string | undefined;
481
+ /**
482
+ * Set the public API key for the Hyphen service. If set, this will also update the underlying services.
483
+ * This is used for public endpoints that do not require authentication such as the Toggle service.
484
+ * @param {string | undefined} value - The public API key to set.
485
+ */
334
486
  set publicApiKey(value: string | undefined);
487
+ /**
488
+ * Get the API key for the Hyphen service.
489
+ * This is used for authenticated endpoints that require an API key such as the NetInfo and Link services.
490
+ * @returns {string | undefined} The API key.
491
+ */
335
492
  get apiKey(): string | undefined;
493
+ /**
494
+ * Set the API key for the Hyphen service. If set, this will also update the underlying services.
495
+ * This is used for authenticated endpoints that require an API key such as the NetInfo and Link services.
496
+ * @param {string | undefined} value - The API key to set.
497
+ */
336
498
  set apiKey(value: string | undefined);
499
+ /**
500
+ * Get whether to throw errors or not.
501
+ * If set to true, errors will be thrown instead of logged.
502
+ * @returns {boolean} Whether to throw errors or not.
503
+ */
337
504
  get throwErrors(): boolean;
505
+ /**
506
+ * Set whether to throw errors or not. If set to true, errors will be thrown instead of logged.
507
+ * This will update the underlying services as well.
508
+ * @param {boolean} value - Whether to throw errors or not.
509
+ */
338
510
  set throwErrors(value: boolean);
339
511
  }
340
512
 
package/dist/index.d.ts CHANGED
@@ -315,26 +315,198 @@ declare class NetInfo extends BaseService {
315
315
  getIpInfos(ips: string[]): Promise<Array<ipInfo | ipInfoError>>;
316
316
  }
317
317
 
318
+ type CreateShortCodeOptions = {
319
+ /**
320
+ * The short code used for this link. If not provided, a random code will be generated.
321
+ * @default undefined
322
+ */
323
+ code?: string;
324
+ /**
325
+ * The title of the link. This is used for display purposes.
326
+ * @default undefined
327
+ */
328
+ title?: string;
329
+ /**
330
+ * The tags associated with the link. This is used for categorization purposes.
331
+ * @default undefined
332
+ */
333
+ tags?: string[];
334
+ };
335
+ type CreateShortCodeResponse = {
336
+ id: string;
337
+ code: string;
338
+ long_url: string;
339
+ domain: string;
340
+ createdAt: string;
341
+ title?: string;
342
+ tags?: string[];
343
+ organizationId: {
344
+ id: string;
345
+ name: string;
346
+ };
347
+ };
348
+ type LinkOptions = {
349
+ /**
350
+ * The URIs to access the link service.
351
+ * @default ["https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"]
352
+ */
353
+ uris?: string[];
354
+ /**
355
+ * The organization ID to use for the link service.
356
+ * @requires organizationId
357
+ */
358
+ organizationId?: string;
359
+ /**
360
+ * The API key to use for the link service. This should be provided as the service requires authentication.
361
+ */
362
+ apiKey?: string;
363
+ } & BaseServiceOptions;
364
+ declare class Link extends BaseService {
365
+ private _uris;
366
+ private _organizationId?;
367
+ private _apiKey?;
368
+ constructor(options?: LinkOptions);
369
+ /**
370
+ * Get the URIs for the link service. The default is `["https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"]`.
371
+ * @returns {string[]} The URIs for the link service.
372
+ */
373
+ get uris(): string[];
374
+ /**
375
+ * Set the URIs for the link service. The default is `["https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"]`.
376
+ * @param {string[]} uris - The URIs to set.
377
+ */
378
+ set uris(uris: string[]);
379
+ /**
380
+ * Get the organization ID for the link service. This is required to access the link service.
381
+ * @returns {string | undefined} The organization ID.
382
+ */
383
+ get organizationId(): string | undefined;
384
+ /**
385
+ * Set the organization ID for the link service. This is required to access the link service.
386
+ * @param {string | undefined} organizationId - The organization ID to set.
387
+ */
388
+ set organizationId(organizationId: string | undefined);
389
+ /**
390
+ * Get the API key for the link service. This is required to access the link service.
391
+ * @returns {string | undefined} The API key.
392
+ */
393
+ get apiKey(): string | undefined;
394
+ /**
395
+ * Set the API key for the link service. This is required to access the link service.
396
+ * @param {string | undefined} apiKey - The API key to set.
397
+ */
398
+ set apiKey(apiKey: string | undefined);
399
+ /**
400
+ * Set the API key for the link service. If the API key starts with 'public_', an error is thrown.
401
+ * This is to ensure that the API key is not a public key, which should not be used for authenticated requests.
402
+ * @param {string} apiKey
403
+ */
404
+ setApiKey(apiKey: string | undefined): void;
405
+ createShortCode(longUrl: string, domain: string, options?: CreateShortCodeOptions): Promise<CreateShortCodeResponse>;
406
+ /**
407
+ * Delete a short code.
408
+ * @param {string} code The short code to delete. Example: 'code_686bed403c3991bd676bba4d'
409
+ * @returns {Promise<boolean>} A promise that resolves to true if the short code was deleted successfully, or false if it was not.
410
+ */
411
+ deleteShortCode(code: string): Promise<boolean>;
412
+ }
413
+
318
414
  type HyphenOptions = {
415
+ /**
416
+ * The public API key to use for the Hyphen service.
417
+ * This is used for public endpoints that do not require authentication.
418
+ */
319
419
  publicApiKey?: string;
420
+ /**
421
+ * The API key to use for the Hyphen service.
422
+ * This is used for authenticated endpoints that require an API key.
423
+ */
320
424
  apiKey?: string;
425
+ /**
426
+ * Whether to throw errors or not.
427
+ * If set to true, errors will be thrown instead of logged.
428
+ * @default false
429
+ */
321
430
  throwErrors?: boolean;
322
- toggle?: Omit<ToggleOptions, 'apiKey' | 'publicApiKey' | 'throwErrors'>;
323
- netInfo?: Omit<NetInfoOptions, 'apiKey' | 'publicApiKey' | 'throwErrors'>;
431
+ /**
432
+ * Options for the Toggle service.
433
+ * Excludes publicApiKey and throwErrors from ToggleOptions.
434
+ * @see ToggleOptions
435
+ * @default {Toggle}
436
+ */
437
+ toggle?: Omit<ToggleOptions, 'publicApiKey' | 'throwErrors'>;
438
+ /**
439
+ * Options for the NetInfo service.
440
+ * Excludes apiKey and throwErrors from NetInfoOptions.
441
+ * @see NetInfoOptions
442
+ * @default {NetInfo}
443
+ */
444
+ netInfo?: Omit<NetInfoOptions, 'apiKey' | 'throwErrors'>;
445
+ /**
446
+ * Options for the Link service.
447
+ * Excludes apiKey and throwErrors from LinkOptions.
448
+ * @see LinkOptions
449
+ * @default {Link}
450
+ */
451
+ link?: Omit<LinkOptions, 'apiKey' | 'throwErrors'>;
324
452
  } & HookifiedOptions;
325
453
  declare class Hyphen extends Hookified {
326
454
  private readonly _netInfo;
327
455
  private readonly _toggle;
456
+ private readonly _link;
328
457
  private _publicApiKey?;
329
458
  private _apiKey?;
330
459
  constructor(options?: HyphenOptions);
460
+ /**
461
+ * Get the NetInfo service instance.
462
+ * @returns {NetInfo} The NetInfo service instance.
463
+ */
331
464
  get netInfo(): NetInfo;
465
+ /**
466
+ * Get the Toggle service instance.
467
+ * @returns {Toggle} The Toggle service instance.
468
+ */
332
469
  get toggle(): Toggle;
470
+ /**
471
+ * Get the Link service instance.
472
+ * @returns {Link} The Link service instance.
473
+ */
474
+ get link(): Link;
475
+ /**
476
+ * Get the public API key for the Hyphen service.
477
+ * This is used for public endpoints that do not require authentication.
478
+ * @returns {string | undefined} The public API key.
479
+ */
333
480
  get publicApiKey(): string | undefined;
481
+ /**
482
+ * Set the public API key for the Hyphen service. If set, this will also update the underlying services.
483
+ * This is used for public endpoints that do not require authentication such as the Toggle service.
484
+ * @param {string | undefined} value - The public API key to set.
485
+ */
334
486
  set publicApiKey(value: string | undefined);
487
+ /**
488
+ * Get the API key for the Hyphen service.
489
+ * This is used for authenticated endpoints that require an API key such as the NetInfo and Link services.
490
+ * @returns {string | undefined} The API key.
491
+ */
335
492
  get apiKey(): string | undefined;
493
+ /**
494
+ * Set the API key for the Hyphen service. If set, this will also update the underlying services.
495
+ * This is used for authenticated endpoints that require an API key such as the NetInfo and Link services.
496
+ * @param {string | undefined} value - The API key to set.
497
+ */
336
498
  set apiKey(value: string | undefined);
499
+ /**
500
+ * Get whether to throw errors or not.
501
+ * If set to true, errors will be thrown instead of logged.
502
+ * @returns {boolean} Whether to throw errors or not.
503
+ */
337
504
  get throwErrors(): boolean;
505
+ /**
506
+ * Set whether to throw errors or not. If set to true, errors will be thrown instead of logged.
507
+ * This will update the underlying services as well.
508
+ * @param {boolean} value - Whether to throw errors or not.
509
+ */
338
510
  set throwErrors(value: boolean);
339
511
  }
340
512
 
package/dist/index.js CHANGED
@@ -624,6 +624,133 @@ var NetInfo = class extends BaseService {
624
624
  }
625
625
  };
626
626
 
627
+ // src/link.ts
628
+ import process4 from "process";
629
+ loadEnv();
630
+ var defaultLinkUris = [
631
+ "https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"
632
+ ];
633
+ var Link = class extends BaseService {
634
+ static {
635
+ __name(this, "Link");
636
+ }
637
+ _uris = defaultLinkUris;
638
+ _organizationId;
639
+ _apiKey;
640
+ constructor(options) {
641
+ super(options);
642
+ this._uris = options?.uris ?? defaultLinkUris;
643
+ this._organizationId = options?.organizationId;
644
+ if (options?.apiKey) {
645
+ this.setApiKey(options.apiKey);
646
+ }
647
+ if (!this._apiKey && process4.env.HYPHEN_API_KEY) {
648
+ this.setApiKey(process4.env.HYPHEN_API_KEY);
649
+ }
650
+ if (!this._organizationId && process4.env.HYPHEN_ORGANIZATION_ID) {
651
+ this._organizationId = process4.env.HYPHEN_ORGANIZATION_ID;
652
+ }
653
+ }
654
+ /**
655
+ * Get the URIs for the link service. The default is `["https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"]`.
656
+ * @returns {string[]} The URIs for the link service.
657
+ */
658
+ get uris() {
659
+ return this._uris;
660
+ }
661
+ /**
662
+ * Set the URIs for the link service. The default is `["https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"]`.
663
+ * @param {string[]} uris - The URIs to set.
664
+ */
665
+ set uris(uris) {
666
+ this._uris = uris;
667
+ }
668
+ /**
669
+ * Get the organization ID for the link service. This is required to access the link service.
670
+ * @returns {string | undefined} The organization ID.
671
+ */
672
+ get organizationId() {
673
+ return this._organizationId;
674
+ }
675
+ /**
676
+ * Set the organization ID for the link service. This is required to access the link service.
677
+ * @param {string | undefined} organizationId - The organization ID to set.
678
+ */
679
+ set organizationId(organizationId) {
680
+ this._organizationId = organizationId;
681
+ }
682
+ /**
683
+ * Get the API key for the link service. This is required to access the link service.
684
+ * @returns {string | undefined} The API key.
685
+ */
686
+ get apiKey() {
687
+ return this._apiKey;
688
+ }
689
+ /**
690
+ * Set the API key for the link service. This is required to access the link service.
691
+ * @param {string | undefined} apiKey - The API key to set.
692
+ */
693
+ set apiKey(apiKey) {
694
+ this.setApiKey(apiKey);
695
+ }
696
+ /**
697
+ * Set the API key for the link service. If the API key starts with 'public_', an error is thrown.
698
+ * This is to ensure that the API key is not a public key, which should not be used for authenticated requests.
699
+ * @param {string} apiKey
700
+ */
701
+ setApiKey(apiKey) {
702
+ if (apiKey?.startsWith("public_")) {
703
+ throw new Error('API key cannot start with "public_"');
704
+ }
705
+ if (apiKey) {
706
+ this._apiKey = apiKey;
707
+ }
708
+ }
709
+ async createShortCode(longUrl, domain, options) {
710
+ if (!this._organizationId) {
711
+ throw new Error("Organization ID is required to create a short code.");
712
+ }
713
+ const url = this._uris[0].replace("{organizationId}", this._organizationId);
714
+ const body = {
715
+ // eslint-disable-next-line @typescript-eslint/naming-convention
716
+ long_url: longUrl,
717
+ domain,
718
+ code: options?.code,
719
+ title: options?.title,
720
+ tags: options?.tags
721
+ };
722
+ const headers = this.createHeaders(this._apiKey);
723
+ const response = await this.post(url, body, {
724
+ headers
725
+ });
726
+ if (response.status === 201) {
727
+ return response.data;
728
+ }
729
+ throw new Error(`Failed to create short code: ${response.statusText}`);
730
+ }
731
+ /**
732
+ * Delete a short code.
733
+ * @param {string} code The short code to delete. Example: 'code_686bed403c3991bd676bba4d'
734
+ * @returns {Promise<boolean>} A promise that resolves to true if the short code was deleted successfully, or false if it was not.
735
+ */
736
+ async deleteShortCode(code) {
737
+ if (!this._organizationId) {
738
+ throw new Error("Organization ID is required to delete a short code.");
739
+ }
740
+ let url = this._uris[0].replace("{organizationId}", this._organizationId);
741
+ url = url.endsWith("/") ? `${url}${code}/` : `${url}/${code}/`;
742
+ const headers = this.createHeaders(this._apiKey);
743
+ delete headers["content-type"];
744
+ const response = await this.delete(url, {
745
+ headers
746
+ });
747
+ if (response.status === 204) {
748
+ return true;
749
+ }
750
+ throw new Error(`Failed to delete short code: ${response.statusText}`);
751
+ }
752
+ };
753
+
627
754
  // src/hyphen.ts
628
755
  var Hyphen = class extends Hookified3 {
629
756
  static {
@@ -631,12 +758,14 @@ var Hyphen = class extends Hookified3 {
631
758
  }
632
759
  _netInfo;
633
760
  _toggle;
761
+ _link;
634
762
  _publicApiKey;
635
763
  _apiKey;
636
764
  constructor(options) {
637
765
  super(options);
638
766
  const toggleOptions = options?.toggle ?? {};
639
767
  const netInfoOptions = options?.netInfo ?? {};
768
+ const linkOptions = options?.link ?? {};
640
769
  if (options?.publicApiKey) {
641
770
  this._publicApiKey = options.publicApiKey;
642
771
  toggleOptions.publicApiKey = options.publicApiKey;
@@ -644,10 +773,12 @@ var Hyphen = class extends Hookified3 {
644
773
  if (options?.apiKey) {
645
774
  this._apiKey = options.apiKey;
646
775
  netInfoOptions.apiKey = options.apiKey;
776
+ linkOptions.apiKey = options.apiKey;
647
777
  }
648
778
  if (options?.throwErrors !== void 0) {
649
779
  toggleOptions.throwErrors = options.throwErrors;
650
780
  netInfoOptions.throwErrors = options.throwErrors;
781
+ linkOptions.throwErrors = options.throwErrors;
651
782
  }
652
783
  this._netInfo = new NetInfo(netInfoOptions);
653
784
  this._netInfo.on("error", (message, ...args) => this.emit("error", message, ...args));
@@ -657,33 +788,84 @@ var Hyphen = class extends Hookified3 {
657
788
  this._toggle.on("error", (message, ...args) => this.emit("error", message, ...args));
658
789
  this._toggle.on("info", (message, ...args) => this.emit("info", message, ...args));
659
790
  this._toggle.on("warn", (message, ...args) => this.emit("warn", message, ...args));
791
+ this._link = new Link(linkOptions);
792
+ this._link.on("error", (message, ...args) => this.emit("error", message, ...args));
793
+ this._link.on("info", (message, ...args) => this.emit("info", message, ...args));
794
+ this._link.on("warn", (message, ...args) => this.emit("warn", message, ...args));
660
795
  }
796
+ /**
797
+ * Get the NetInfo service instance.
798
+ * @returns {NetInfo} The NetInfo service instance.
799
+ */
661
800
  get netInfo() {
662
801
  return this._netInfo;
663
802
  }
803
+ /**
804
+ * Get the Toggle service instance.
805
+ * @returns {Toggle} The Toggle service instance.
806
+ */
664
807
  get toggle() {
665
808
  return this._toggle;
666
809
  }
810
+ /**
811
+ * Get the Link service instance.
812
+ * @returns {Link} The Link service instance.
813
+ */
814
+ get link() {
815
+ return this._link;
816
+ }
817
+ /**
818
+ * Get the public API key for the Hyphen service.
819
+ * This is used for public endpoints that do not require authentication.
820
+ * @returns {string | undefined} The public API key.
821
+ */
667
822
  get publicApiKey() {
668
823
  return this._publicApiKey;
669
824
  }
825
+ /**
826
+ * Set the public API key for the Hyphen service. If set, this will also update the underlying services.
827
+ * This is used for public endpoints that do not require authentication such as the Toggle service.
828
+ * @param {string | undefined} value - The public API key to set.
829
+ */
670
830
  set publicApiKey(value) {
671
831
  this._publicApiKey = value;
672
832
  this._toggle.publicApiKey = value;
673
833
  }
834
+ /**
835
+ * Get the API key for the Hyphen service.
836
+ * This is used for authenticated endpoints that require an API key such as the NetInfo and Link services.
837
+ * @returns {string | undefined} The API key.
838
+ */
674
839
  get apiKey() {
675
840
  return this._apiKey;
676
841
  }
842
+ /**
843
+ * Set the API key for the Hyphen service. If set, this will also update the underlying services.
844
+ * This is used for authenticated endpoints that require an API key such as the NetInfo and Link services.
845
+ * @param {string | undefined} value - The API key to set.
846
+ */
677
847
  set apiKey(value) {
678
848
  this._apiKey = value;
679
849
  this._netInfo.apiKey = value;
850
+ this._link.apiKey = value;
680
851
  }
852
+ /**
853
+ * Get whether to throw errors or not.
854
+ * If set to true, errors will be thrown instead of logged.
855
+ * @returns {boolean} Whether to throw errors or not.
856
+ */
681
857
  get throwErrors() {
682
- return this._netInfo.throwErrors && this._toggle.throwErrors;
858
+ return this._netInfo.throwErrors && this._toggle.throwErrors && this._link.throwErrors;
683
859
  }
860
+ /**
861
+ * Set whether to throw errors or not. If set to true, errors will be thrown instead of logged.
862
+ * This will update the underlying services as well.
863
+ * @param {boolean} value - Whether to throw errors or not.
864
+ */
684
865
  set throwErrors(value) {
685
866
  this._netInfo.throwErrors = value;
686
867
  this._toggle.throwErrors = value;
868
+ this._link.throwErrors = value;
687
869
  }
688
870
  };
689
871
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyphen/sdk",
3
- "version": "1.9.0",
3
+ "version": "1.10.0",
4
4
  "description": "Hyphen SDK for Node.js",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",