@hyphen/sdk 1.9.0 → 1.11.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 +91 -7
- package/dist/index.cjs +241 -1
- package/dist/index.d.cts +195 -2
- package/dist/index.d.ts +195 -2
- package/dist/index.js +241 -1
- package/package.json +9 -9
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,14 @@ 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)
|
|
27
|
+
- [Creating a Short Code](#creating-a-short-code)
|
|
28
|
+
- [Getting a Short Code](#getting-a-short-code)
|
|
29
|
+
- [Getting Short Codes](#getting-short-codes)
|
|
30
|
+
- [Deleting a Short Code](#deleting-a-short-code)
|
|
26
31
|
- [Contributing](#contributing)
|
|
27
32
|
- [Testing Your Changes](#testing-your-changes)
|
|
28
33
|
- [License and Copyright](#license-and-copyright)
|
|
@@ -123,7 +128,7 @@ console.log('Geo IP information:', result);
|
|
|
123
128
|
|
|
124
129
|
The rest of the examples for each service show you accessing the service instance directly.
|
|
125
130
|
|
|
126
|
-
# Toggle
|
|
131
|
+
# Toggle - Feature Flag Service
|
|
127
132
|
|
|
128
133
|
[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
134
|
|
|
@@ -571,7 +576,7 @@ const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
|
|
|
571
576
|
console.log('Boolean toggle value:', result); // true
|
|
572
577
|
```
|
|
573
578
|
|
|
574
|
-
# ENV
|
|
579
|
+
# ENV - Secret Management Service
|
|
575
580
|
|
|
576
581
|
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
582
|
|
|
@@ -616,7 +621,7 @@ import { loadEnv } from '@hyphen/sdk';
|
|
|
616
621
|
loadEnv({ local: false });
|
|
617
622
|
```
|
|
618
623
|
|
|
619
|
-
# Net Info
|
|
624
|
+
# Net Info - Geo Information Service
|
|
620
625
|
|
|
621
626
|
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
627
|
|
|
@@ -650,6 +655,75 @@ console.log('IP Infos:', ipInfos);
|
|
|
650
655
|
|
|
651
656
|
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
657
|
|
|
658
|
+
# Link - Short Code Service
|
|
659
|
+
|
|
660
|
+
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.
|
|
661
|
+
|
|
662
|
+
* [Website](https://hyphen.ai/link)
|
|
663
|
+
* [Guides](https://docs.hyphen.ai/docs/create-short-link)
|
|
664
|
+
* [API Reference](https://docs.hyphen.ai/reference/post_api-organizations-organizationid-link-codes)
|
|
665
|
+
|
|
666
|
+
## Creating a Short Code
|
|
667
|
+
To create a short code, you can use the `createShortCode` method:
|
|
668
|
+
|
|
669
|
+
```javascript
|
|
670
|
+
import { Link } from '@hyphen/sdk';
|
|
671
|
+
const link = new Link({
|
|
672
|
+
organizationId: 'your_organization_id',
|
|
673
|
+
apiKey: 'your_api_key',
|
|
674
|
+
});
|
|
675
|
+
const longUrl = 'https://hyphen.ai';
|
|
676
|
+
const domain = 'test.h4n.link';
|
|
677
|
+
const options = {
|
|
678
|
+
tags: ['sdk-test', 'unit-test'],
|
|
679
|
+
};
|
|
680
|
+
const response = await link.createShortCode(longUrl, domain, options);
|
|
681
|
+
console.log('Short Code Response:', response);
|
|
682
|
+
```
|
|
683
|
+
|
|
684
|
+
## Getting a Short Code
|
|
685
|
+
To get a short code, you can use the `getShortCode` method:
|
|
686
|
+
|
|
687
|
+
```javascript
|
|
688
|
+
import { Link } from '@hyphen/sdk';
|
|
689
|
+
const link = new Link({
|
|
690
|
+
organizationId: 'your_organization_id',
|
|
691
|
+
apiKey: 'your_api_key',
|
|
692
|
+
});
|
|
693
|
+
const code = 'code_1234567890'; // It is the code identifier for the short code you want to get
|
|
694
|
+
const response = await link.getShortCode(code);
|
|
695
|
+
console.log('Get Short Code Response:', response);
|
|
696
|
+
```
|
|
697
|
+
|
|
698
|
+
## Getting Short Codes
|
|
699
|
+
To get a list of short codes, you can use the `getShortCodes` method:
|
|
700
|
+
|
|
701
|
+
```javascript
|
|
702
|
+
import { Link } from '@hyphen/sdk';
|
|
703
|
+
const link = new Link({
|
|
704
|
+
organizationId: 'your_organization_id',
|
|
705
|
+
apiKey: 'your_api_key',
|
|
706
|
+
});
|
|
707
|
+
const title = 'My Short Codes'; // Optional title to filter short codes
|
|
708
|
+
const tags = ['sdk-test', 'unit-test']; // Optional tags to filter short codes
|
|
709
|
+
const response = await link.getShortCodes(title, tags);
|
|
710
|
+
console.log('Get Short Codes Response:', response);
|
|
711
|
+
```
|
|
712
|
+
|
|
713
|
+
## Deleting a Short Code
|
|
714
|
+
if you want to delete a short code you can do it like this:
|
|
715
|
+
|
|
716
|
+
```javascript
|
|
717
|
+
import { Link } from '@hyphen/sdk';
|
|
718
|
+
const link = new Link({
|
|
719
|
+
organizationId: 'your_organization_id',
|
|
720
|
+
apiKey: 'your_api_key',
|
|
721
|
+
});
|
|
722
|
+
const code = 'code_1234567890'; // It is the code identifier for the short code you want to delete
|
|
723
|
+
const response = await link.deleteShortCode(code);
|
|
724
|
+
console.log('Delete Short Code Response:', response);
|
|
725
|
+
```
|
|
726
|
+
|
|
653
727
|
# Contributing
|
|
654
728
|
|
|
655
729
|
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 +764,19 @@ Once you have created the project, added the toggles, and created your applicati
|
|
|
690
764
|
HYPHEN_PUBLIC_API_KEY=your_public_api_key
|
|
691
765
|
HYPHEN_API_KEY=your_api_key
|
|
692
766
|
HYPHEN_APPLICATION_ID=your_project_id
|
|
767
|
+
HYPHEN_LINK_DOMAIN=your_link_domain
|
|
768
|
+
HYPHEN_ORGANIZATION_ID=your_organization_id
|
|
693
769
|
```
|
|
694
770
|
|
|
695
|
-
|
|
771
|
+
A bit more information about the environment variables:
|
|
772
|
+
|
|
773
|
+
| Variable | Example Value | Description |
|
|
774
|
+
|----------------|----------------|----------------|
|
|
775
|
+
| *HYPHEN_PUBLIC_API_KEY* | `public_api_key` | The public API key for your Hyphen project. You can find this in the Hyphen dashboard. |
|
|
776
|
+
| *HYPHEN_API_KEY* | `api_key` | The API key for your Hyphen project. You can find this in the Hyphen dashboard. |
|
|
777
|
+
| *HYPHEN_APPLICATION_ID* | `application_id` | The application ID for your Hyphen project. You can find this in the Hyphen dashboard. |
|
|
778
|
+
| *HYPHEN_LINK_DOMAIN* | `test.h4n.link` | The domain for the Link service. This is used for generating links. |
|
|
779
|
+
| *HYPHEN_ORGANIZATION_ID* | `org_668575c0e169cde974a5c76a` | | The organization ID for your Hyphen project. This is used for the Link service. |
|
|
696
780
|
|
|
697
781
|
Then run the tests with the following command:
|
|
698
782
|
|
package/dist/index.cjs
CHANGED
|
@@ -661,6 +661,191 @@ 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
|
+
/**
|
|
747
|
+
* Get the URI for a specific organization and code. This is used internally to construct the URI for the link service.
|
|
748
|
+
* @param {string} organizationId The ID of the organization.
|
|
749
|
+
* @param {string} code The code to include in the URI.
|
|
750
|
+
* @returns {string} The constructed URI.
|
|
751
|
+
*/
|
|
752
|
+
getUri(organizationId, code) {
|
|
753
|
+
if (!organizationId) {
|
|
754
|
+
throw new Error("Organization ID is required to get the URI.");
|
|
755
|
+
}
|
|
756
|
+
let url = this._uris[0].replace("{organizationId}", organizationId);
|
|
757
|
+
if (code) {
|
|
758
|
+
url = url.endsWith("/") ? `${url}${code}/` : `${url}/${code}/`;
|
|
759
|
+
}
|
|
760
|
+
return url;
|
|
761
|
+
}
|
|
762
|
+
async createShortCode(longUrl, domain, options) {
|
|
763
|
+
if (!this._organizationId) {
|
|
764
|
+
throw new Error("Organization ID is required to create a short code.");
|
|
765
|
+
}
|
|
766
|
+
const url = this.getUri(this._organizationId);
|
|
767
|
+
const body = {
|
|
768
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
769
|
+
long_url: longUrl,
|
|
770
|
+
domain,
|
|
771
|
+
code: options?.code,
|
|
772
|
+
title: options?.title,
|
|
773
|
+
tags: options?.tags
|
|
774
|
+
};
|
|
775
|
+
const headers = this.createHeaders(this._apiKey);
|
|
776
|
+
const response = await this.post(url, body, {
|
|
777
|
+
headers
|
|
778
|
+
});
|
|
779
|
+
if (response.status === 201) {
|
|
780
|
+
return response.data;
|
|
781
|
+
}
|
|
782
|
+
throw new Error(`Failed to create short code: ${response.statusText}`);
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* Get a short code by its code.
|
|
786
|
+
* @param {string} code The short code to retrieve. Example: 'code_686bed403c3991bd676bba4d'
|
|
787
|
+
* @returns {Promise<GetShortCodeResponse>} A promise that resolves to the short code details.
|
|
788
|
+
*/
|
|
789
|
+
async getShortCode(code) {
|
|
790
|
+
if (!this._organizationId) {
|
|
791
|
+
throw new Error("Organization ID is required to get a short code.");
|
|
792
|
+
}
|
|
793
|
+
const url = this.getUri(this._organizationId, code);
|
|
794
|
+
const headers = this.createHeaders(this._apiKey);
|
|
795
|
+
const response = await this.get(url, {
|
|
796
|
+
headers
|
|
797
|
+
});
|
|
798
|
+
if (response.status === 200) {
|
|
799
|
+
return response.data;
|
|
800
|
+
}
|
|
801
|
+
throw new Error(`Failed to get short code: ${response.statusText}`);
|
|
802
|
+
}
|
|
803
|
+
async getShortCodes(titleSearch, tags, pageNumber = 1, pageSize = 100) {
|
|
804
|
+
if (!this._organizationId) {
|
|
805
|
+
throw new Error("Organization ID is required to get short codes.");
|
|
806
|
+
}
|
|
807
|
+
const url = this.getUri(this._organizationId);
|
|
808
|
+
const headers = this.createHeaders(this._apiKey);
|
|
809
|
+
const parameters = {};
|
|
810
|
+
if (titleSearch) {
|
|
811
|
+
parameters.title = titleSearch;
|
|
812
|
+
}
|
|
813
|
+
if (tags && tags.length > 0) {
|
|
814
|
+
parameters.tags = tags.join(",");
|
|
815
|
+
}
|
|
816
|
+
parameters.pageNum = pageNumber.toString();
|
|
817
|
+
parameters.pageSize = pageSize.toString();
|
|
818
|
+
const response = await this.get(url, {
|
|
819
|
+
headers,
|
|
820
|
+
params: parameters
|
|
821
|
+
});
|
|
822
|
+
if (response.status === 200) {
|
|
823
|
+
return response.data;
|
|
824
|
+
}
|
|
825
|
+
throw new Error(`Failed to get short codes: ${response.statusText}`);
|
|
826
|
+
}
|
|
827
|
+
/**
|
|
828
|
+
* Delete a short code.
|
|
829
|
+
* @param {string} code The short code to delete. Example: 'code_686bed403c3991bd676bba4d'
|
|
830
|
+
* @returns {Promise<boolean>} A promise that resolves to true if the short code was deleted successfully, or false if it was not.
|
|
831
|
+
*/
|
|
832
|
+
async deleteShortCode(code) {
|
|
833
|
+
if (!this._organizationId) {
|
|
834
|
+
throw new Error("Organization ID is required to delete a short code.");
|
|
835
|
+
}
|
|
836
|
+
const url = this.getUri(this._organizationId, code);
|
|
837
|
+
const headers = this.createHeaders(this._apiKey);
|
|
838
|
+
delete headers["content-type"];
|
|
839
|
+
const response = await this.delete(url, {
|
|
840
|
+
headers
|
|
841
|
+
});
|
|
842
|
+
if (response.status === 204) {
|
|
843
|
+
return true;
|
|
844
|
+
}
|
|
845
|
+
throw new Error(`Failed to delete short code: ${response.statusText}`);
|
|
846
|
+
}
|
|
847
|
+
};
|
|
848
|
+
|
|
664
849
|
// src/hyphen.ts
|
|
665
850
|
var Hyphen = class extends import_hookified3.Hookified {
|
|
666
851
|
static {
|
|
@@ -668,12 +853,14 @@ var Hyphen = class extends import_hookified3.Hookified {
|
|
|
668
853
|
}
|
|
669
854
|
_netInfo;
|
|
670
855
|
_toggle;
|
|
856
|
+
_link;
|
|
671
857
|
_publicApiKey;
|
|
672
858
|
_apiKey;
|
|
673
859
|
constructor(options) {
|
|
674
860
|
super(options);
|
|
675
861
|
const toggleOptions = options?.toggle ?? {};
|
|
676
862
|
const netInfoOptions = options?.netInfo ?? {};
|
|
863
|
+
const linkOptions = options?.link ?? {};
|
|
677
864
|
if (options?.publicApiKey) {
|
|
678
865
|
this._publicApiKey = options.publicApiKey;
|
|
679
866
|
toggleOptions.publicApiKey = options.publicApiKey;
|
|
@@ -681,10 +868,12 @@ var Hyphen = class extends import_hookified3.Hookified {
|
|
|
681
868
|
if (options?.apiKey) {
|
|
682
869
|
this._apiKey = options.apiKey;
|
|
683
870
|
netInfoOptions.apiKey = options.apiKey;
|
|
871
|
+
linkOptions.apiKey = options.apiKey;
|
|
684
872
|
}
|
|
685
873
|
if (options?.throwErrors !== void 0) {
|
|
686
874
|
toggleOptions.throwErrors = options.throwErrors;
|
|
687
875
|
netInfoOptions.throwErrors = options.throwErrors;
|
|
876
|
+
linkOptions.throwErrors = options.throwErrors;
|
|
688
877
|
}
|
|
689
878
|
this._netInfo = new NetInfo(netInfoOptions);
|
|
690
879
|
this._netInfo.on("error", (message, ...args) => this.emit("error", message, ...args));
|
|
@@ -694,33 +883,84 @@ var Hyphen = class extends import_hookified3.Hookified {
|
|
|
694
883
|
this._toggle.on("error", (message, ...args) => this.emit("error", message, ...args));
|
|
695
884
|
this._toggle.on("info", (message, ...args) => this.emit("info", message, ...args));
|
|
696
885
|
this._toggle.on("warn", (message, ...args) => this.emit("warn", message, ...args));
|
|
886
|
+
this._link = new Link(linkOptions);
|
|
887
|
+
this._link.on("error", (message, ...args) => this.emit("error", message, ...args));
|
|
888
|
+
this._link.on("info", (message, ...args) => this.emit("info", message, ...args));
|
|
889
|
+
this._link.on("warn", (message, ...args) => this.emit("warn", message, ...args));
|
|
697
890
|
}
|
|
891
|
+
/**
|
|
892
|
+
* Get the NetInfo service instance.
|
|
893
|
+
* @returns {NetInfo} The NetInfo service instance.
|
|
894
|
+
*/
|
|
698
895
|
get netInfo() {
|
|
699
896
|
return this._netInfo;
|
|
700
897
|
}
|
|
898
|
+
/**
|
|
899
|
+
* Get the Toggle service instance.
|
|
900
|
+
* @returns {Toggle} The Toggle service instance.
|
|
901
|
+
*/
|
|
701
902
|
get toggle() {
|
|
702
903
|
return this._toggle;
|
|
703
904
|
}
|
|
905
|
+
/**
|
|
906
|
+
* Get the Link service instance.
|
|
907
|
+
* @returns {Link} The Link service instance.
|
|
908
|
+
*/
|
|
909
|
+
get link() {
|
|
910
|
+
return this._link;
|
|
911
|
+
}
|
|
912
|
+
/**
|
|
913
|
+
* Get the public API key for the Hyphen service.
|
|
914
|
+
* This is used for public endpoints that do not require authentication.
|
|
915
|
+
* @returns {string | undefined} The public API key.
|
|
916
|
+
*/
|
|
704
917
|
get publicApiKey() {
|
|
705
918
|
return this._publicApiKey;
|
|
706
919
|
}
|
|
920
|
+
/**
|
|
921
|
+
* Set the public API key for the Hyphen service. If set, this will also update the underlying services.
|
|
922
|
+
* This is used for public endpoints that do not require authentication such as the Toggle service.
|
|
923
|
+
* @param {string | undefined} value - The public API key to set.
|
|
924
|
+
*/
|
|
707
925
|
set publicApiKey(value) {
|
|
708
926
|
this._publicApiKey = value;
|
|
709
927
|
this._toggle.publicApiKey = value;
|
|
710
928
|
}
|
|
929
|
+
/**
|
|
930
|
+
* Get the API key for the Hyphen service.
|
|
931
|
+
* This is used for authenticated endpoints that require an API key such as the NetInfo and Link services.
|
|
932
|
+
* @returns {string | undefined} The API key.
|
|
933
|
+
*/
|
|
711
934
|
get apiKey() {
|
|
712
935
|
return this._apiKey;
|
|
713
936
|
}
|
|
937
|
+
/**
|
|
938
|
+
* Set the API key for the Hyphen service. If set, this will also update the underlying services.
|
|
939
|
+
* This is used for authenticated endpoints that require an API key such as the NetInfo and Link services.
|
|
940
|
+
* @param {string | undefined} value - The API key to set.
|
|
941
|
+
*/
|
|
714
942
|
set apiKey(value) {
|
|
715
943
|
this._apiKey = value;
|
|
716
944
|
this._netInfo.apiKey = value;
|
|
945
|
+
this._link.apiKey = value;
|
|
717
946
|
}
|
|
947
|
+
/**
|
|
948
|
+
* Get whether to throw errors or not.
|
|
949
|
+
* If set to true, errors will be thrown instead of logged.
|
|
950
|
+
* @returns {boolean} Whether to throw errors or not.
|
|
951
|
+
*/
|
|
718
952
|
get throwErrors() {
|
|
719
|
-
return this._netInfo.throwErrors && this._toggle.throwErrors;
|
|
953
|
+
return this._netInfo.throwErrors && this._toggle.throwErrors && this._link.throwErrors;
|
|
720
954
|
}
|
|
955
|
+
/**
|
|
956
|
+
* Set whether to throw errors or not. If set to true, errors will be thrown instead of logged.
|
|
957
|
+
* This will update the underlying services as well.
|
|
958
|
+
* @param {boolean} value - Whether to throw errors or not.
|
|
959
|
+
*/
|
|
721
960
|
set throwErrors(value) {
|
|
722
961
|
this._netInfo.throwErrors = value;
|
|
723
962
|
this._toggle.throwErrors = value;
|
|
963
|
+
this._link.throwErrors = value;
|
|
724
964
|
}
|
|
725
965
|
};
|
|
726
966
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/index.d.cts
CHANGED
|
@@ -315,26 +315,219 @@ 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 GetShortCodesResponse = {
|
|
349
|
+
total: number;
|
|
350
|
+
pageNum: number;
|
|
351
|
+
pageSize: number;
|
|
352
|
+
data: GetShortCodeResponse[];
|
|
353
|
+
};
|
|
354
|
+
type GetShortCodeResponse = CreateShortCodeResponse;
|
|
355
|
+
type LinkOptions = {
|
|
356
|
+
/**
|
|
357
|
+
* The URIs to access the link service.
|
|
358
|
+
* @default ["https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"]
|
|
359
|
+
*/
|
|
360
|
+
uris?: string[];
|
|
361
|
+
/**
|
|
362
|
+
* The organization ID to use for the link service.
|
|
363
|
+
* @requires organizationId
|
|
364
|
+
*/
|
|
365
|
+
organizationId?: string;
|
|
366
|
+
/**
|
|
367
|
+
* The API key to use for the link service. This should be provided as the service requires authentication.
|
|
368
|
+
*/
|
|
369
|
+
apiKey?: string;
|
|
370
|
+
} & BaseServiceOptions;
|
|
371
|
+
declare class Link extends BaseService {
|
|
372
|
+
private _uris;
|
|
373
|
+
private _organizationId?;
|
|
374
|
+
private _apiKey?;
|
|
375
|
+
constructor(options?: LinkOptions);
|
|
376
|
+
/**
|
|
377
|
+
* Get the URIs for the link service. The default is `["https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"]`.
|
|
378
|
+
* @returns {string[]} The URIs for the link service.
|
|
379
|
+
*/
|
|
380
|
+
get uris(): string[];
|
|
381
|
+
/**
|
|
382
|
+
* Set the URIs for the link service. The default is `["https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"]`.
|
|
383
|
+
* @param {string[]} uris - The URIs to set.
|
|
384
|
+
*/
|
|
385
|
+
set uris(uris: string[]);
|
|
386
|
+
/**
|
|
387
|
+
* Get the organization ID for the link service. This is required to access the link service.
|
|
388
|
+
* @returns {string | undefined} The organization ID.
|
|
389
|
+
*/
|
|
390
|
+
get organizationId(): string | undefined;
|
|
391
|
+
/**
|
|
392
|
+
* Set the organization ID for the link service. This is required to access the link service.
|
|
393
|
+
* @param {string | undefined} organizationId - The organization ID to set.
|
|
394
|
+
*/
|
|
395
|
+
set organizationId(organizationId: string | undefined);
|
|
396
|
+
/**
|
|
397
|
+
* Get the API key for the link service. This is required to access the link service.
|
|
398
|
+
* @returns {string | undefined} The API key.
|
|
399
|
+
*/
|
|
400
|
+
get apiKey(): string | undefined;
|
|
401
|
+
/**
|
|
402
|
+
* Set the API key for the link service. This is required to access the link service.
|
|
403
|
+
* @param {string | undefined} apiKey - The API key to set.
|
|
404
|
+
*/
|
|
405
|
+
set apiKey(apiKey: string | undefined);
|
|
406
|
+
/**
|
|
407
|
+
* Set the API key for the link service. If the API key starts with 'public_', an error is thrown.
|
|
408
|
+
* This is to ensure that the API key is not a public key, which should not be used for authenticated requests.
|
|
409
|
+
* @param {string} apiKey
|
|
410
|
+
*/
|
|
411
|
+
setApiKey(apiKey: string | undefined): void;
|
|
412
|
+
/**
|
|
413
|
+
* Get the URI for a specific organization and code. This is used internally to construct the URI for the link service.
|
|
414
|
+
* @param {string} organizationId The ID of the organization.
|
|
415
|
+
* @param {string} code The code to include in the URI.
|
|
416
|
+
* @returns {string} The constructed URI.
|
|
417
|
+
*/
|
|
418
|
+
getUri(organizationId: string, code?: string): string;
|
|
419
|
+
createShortCode(longUrl: string, domain: string, options?: CreateShortCodeOptions): Promise<CreateShortCodeResponse>;
|
|
420
|
+
/**
|
|
421
|
+
* Get a short code by its code.
|
|
422
|
+
* @param {string} code The short code to retrieve. Example: 'code_686bed403c3991bd676bba4d'
|
|
423
|
+
* @returns {Promise<GetShortCodeResponse>} A promise that resolves to the short code details.
|
|
424
|
+
*/
|
|
425
|
+
getShortCode(code: string): Promise<GetShortCodeResponse>;
|
|
426
|
+
getShortCodes(titleSearch: string, tags?: string[], pageNumber?: number, pageSize?: number): Promise<GetShortCodesResponse>;
|
|
427
|
+
/**
|
|
428
|
+
* Delete a short code.
|
|
429
|
+
* @param {string} code The short code to delete. Example: 'code_686bed403c3991bd676bba4d'
|
|
430
|
+
* @returns {Promise<boolean>} A promise that resolves to true if the short code was deleted successfully, or false if it was not.
|
|
431
|
+
*/
|
|
432
|
+
deleteShortCode(code: string): Promise<boolean>;
|
|
433
|
+
}
|
|
434
|
+
|
|
318
435
|
type HyphenOptions = {
|
|
436
|
+
/**
|
|
437
|
+
* The public API key to use for the Hyphen service.
|
|
438
|
+
* This is used for public endpoints that do not require authentication.
|
|
439
|
+
*/
|
|
319
440
|
publicApiKey?: string;
|
|
441
|
+
/**
|
|
442
|
+
* The API key to use for the Hyphen service.
|
|
443
|
+
* This is used for authenticated endpoints that require an API key.
|
|
444
|
+
*/
|
|
320
445
|
apiKey?: string;
|
|
446
|
+
/**
|
|
447
|
+
* Whether to throw errors or not.
|
|
448
|
+
* If set to true, errors will be thrown instead of logged.
|
|
449
|
+
* @default false
|
|
450
|
+
*/
|
|
321
451
|
throwErrors?: boolean;
|
|
322
|
-
|
|
323
|
-
|
|
452
|
+
/**
|
|
453
|
+
* Options for the Toggle service.
|
|
454
|
+
* Excludes publicApiKey and throwErrors from ToggleOptions.
|
|
455
|
+
* @see ToggleOptions
|
|
456
|
+
* @default {Toggle}
|
|
457
|
+
*/
|
|
458
|
+
toggle?: Omit<ToggleOptions, 'publicApiKey' | 'throwErrors'>;
|
|
459
|
+
/**
|
|
460
|
+
* Options for the NetInfo service.
|
|
461
|
+
* Excludes apiKey and throwErrors from NetInfoOptions.
|
|
462
|
+
* @see NetInfoOptions
|
|
463
|
+
* @default {NetInfo}
|
|
464
|
+
*/
|
|
465
|
+
netInfo?: Omit<NetInfoOptions, 'apiKey' | 'throwErrors'>;
|
|
466
|
+
/**
|
|
467
|
+
* Options for the Link service.
|
|
468
|
+
* Excludes apiKey and throwErrors from LinkOptions.
|
|
469
|
+
* @see LinkOptions
|
|
470
|
+
* @default {Link}
|
|
471
|
+
*/
|
|
472
|
+
link?: Omit<LinkOptions, 'apiKey' | 'throwErrors'>;
|
|
324
473
|
} & HookifiedOptions;
|
|
325
474
|
declare class Hyphen extends Hookified {
|
|
326
475
|
private readonly _netInfo;
|
|
327
476
|
private readonly _toggle;
|
|
477
|
+
private readonly _link;
|
|
328
478
|
private _publicApiKey?;
|
|
329
479
|
private _apiKey?;
|
|
330
480
|
constructor(options?: HyphenOptions);
|
|
481
|
+
/**
|
|
482
|
+
* Get the NetInfo service instance.
|
|
483
|
+
* @returns {NetInfo} The NetInfo service instance.
|
|
484
|
+
*/
|
|
331
485
|
get netInfo(): NetInfo;
|
|
486
|
+
/**
|
|
487
|
+
* Get the Toggle service instance.
|
|
488
|
+
* @returns {Toggle} The Toggle service instance.
|
|
489
|
+
*/
|
|
332
490
|
get toggle(): Toggle;
|
|
491
|
+
/**
|
|
492
|
+
* Get the Link service instance.
|
|
493
|
+
* @returns {Link} The Link service instance.
|
|
494
|
+
*/
|
|
495
|
+
get link(): Link;
|
|
496
|
+
/**
|
|
497
|
+
* Get the public API key for the Hyphen service.
|
|
498
|
+
* This is used for public endpoints that do not require authentication.
|
|
499
|
+
* @returns {string | undefined} The public API key.
|
|
500
|
+
*/
|
|
333
501
|
get publicApiKey(): string | undefined;
|
|
502
|
+
/**
|
|
503
|
+
* Set the public API key for the Hyphen service. If set, this will also update the underlying services.
|
|
504
|
+
* This is used for public endpoints that do not require authentication such as the Toggle service.
|
|
505
|
+
* @param {string | undefined} value - The public API key to set.
|
|
506
|
+
*/
|
|
334
507
|
set publicApiKey(value: string | undefined);
|
|
508
|
+
/**
|
|
509
|
+
* Get the API key for the Hyphen service.
|
|
510
|
+
* This is used for authenticated endpoints that require an API key such as the NetInfo and Link services.
|
|
511
|
+
* @returns {string | undefined} The API key.
|
|
512
|
+
*/
|
|
335
513
|
get apiKey(): string | undefined;
|
|
514
|
+
/**
|
|
515
|
+
* Set the API key for the Hyphen service. If set, this will also update the underlying services.
|
|
516
|
+
* This is used for authenticated endpoints that require an API key such as the NetInfo and Link services.
|
|
517
|
+
* @param {string | undefined} value - The API key to set.
|
|
518
|
+
*/
|
|
336
519
|
set apiKey(value: string | undefined);
|
|
520
|
+
/**
|
|
521
|
+
* Get whether to throw errors or not.
|
|
522
|
+
* If set to true, errors will be thrown instead of logged.
|
|
523
|
+
* @returns {boolean} Whether to throw errors or not.
|
|
524
|
+
*/
|
|
337
525
|
get throwErrors(): boolean;
|
|
526
|
+
/**
|
|
527
|
+
* Set whether to throw errors or not. If set to true, errors will be thrown instead of logged.
|
|
528
|
+
* This will update the underlying services as well.
|
|
529
|
+
* @param {boolean} value - Whether to throw errors or not.
|
|
530
|
+
*/
|
|
338
531
|
set throwErrors(value: boolean);
|
|
339
532
|
}
|
|
340
533
|
|
package/dist/index.d.ts
CHANGED
|
@@ -315,26 +315,219 @@ 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 GetShortCodesResponse = {
|
|
349
|
+
total: number;
|
|
350
|
+
pageNum: number;
|
|
351
|
+
pageSize: number;
|
|
352
|
+
data: GetShortCodeResponse[];
|
|
353
|
+
};
|
|
354
|
+
type GetShortCodeResponse = CreateShortCodeResponse;
|
|
355
|
+
type LinkOptions = {
|
|
356
|
+
/**
|
|
357
|
+
* The URIs to access the link service.
|
|
358
|
+
* @default ["https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"]
|
|
359
|
+
*/
|
|
360
|
+
uris?: string[];
|
|
361
|
+
/**
|
|
362
|
+
* The organization ID to use for the link service.
|
|
363
|
+
* @requires organizationId
|
|
364
|
+
*/
|
|
365
|
+
organizationId?: string;
|
|
366
|
+
/**
|
|
367
|
+
* The API key to use for the link service. This should be provided as the service requires authentication.
|
|
368
|
+
*/
|
|
369
|
+
apiKey?: string;
|
|
370
|
+
} & BaseServiceOptions;
|
|
371
|
+
declare class Link extends BaseService {
|
|
372
|
+
private _uris;
|
|
373
|
+
private _organizationId?;
|
|
374
|
+
private _apiKey?;
|
|
375
|
+
constructor(options?: LinkOptions);
|
|
376
|
+
/**
|
|
377
|
+
* Get the URIs for the link service. The default is `["https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"]`.
|
|
378
|
+
* @returns {string[]} The URIs for the link service.
|
|
379
|
+
*/
|
|
380
|
+
get uris(): string[];
|
|
381
|
+
/**
|
|
382
|
+
* Set the URIs for the link service. The default is `["https://api.hyphen.ai/api/organizations/{organizationId}/link/codes/"]`.
|
|
383
|
+
* @param {string[]} uris - The URIs to set.
|
|
384
|
+
*/
|
|
385
|
+
set uris(uris: string[]);
|
|
386
|
+
/**
|
|
387
|
+
* Get the organization ID for the link service. This is required to access the link service.
|
|
388
|
+
* @returns {string | undefined} The organization ID.
|
|
389
|
+
*/
|
|
390
|
+
get organizationId(): string | undefined;
|
|
391
|
+
/**
|
|
392
|
+
* Set the organization ID for the link service. This is required to access the link service.
|
|
393
|
+
* @param {string | undefined} organizationId - The organization ID to set.
|
|
394
|
+
*/
|
|
395
|
+
set organizationId(organizationId: string | undefined);
|
|
396
|
+
/**
|
|
397
|
+
* Get the API key for the link service. This is required to access the link service.
|
|
398
|
+
* @returns {string | undefined} The API key.
|
|
399
|
+
*/
|
|
400
|
+
get apiKey(): string | undefined;
|
|
401
|
+
/**
|
|
402
|
+
* Set the API key for the link service. This is required to access the link service.
|
|
403
|
+
* @param {string | undefined} apiKey - The API key to set.
|
|
404
|
+
*/
|
|
405
|
+
set apiKey(apiKey: string | undefined);
|
|
406
|
+
/**
|
|
407
|
+
* Set the API key for the link service. If the API key starts with 'public_', an error is thrown.
|
|
408
|
+
* This is to ensure that the API key is not a public key, which should not be used for authenticated requests.
|
|
409
|
+
* @param {string} apiKey
|
|
410
|
+
*/
|
|
411
|
+
setApiKey(apiKey: string | undefined): void;
|
|
412
|
+
/**
|
|
413
|
+
* Get the URI for a specific organization and code. This is used internally to construct the URI for the link service.
|
|
414
|
+
* @param {string} organizationId The ID of the organization.
|
|
415
|
+
* @param {string} code The code to include in the URI.
|
|
416
|
+
* @returns {string} The constructed URI.
|
|
417
|
+
*/
|
|
418
|
+
getUri(organizationId: string, code?: string): string;
|
|
419
|
+
createShortCode(longUrl: string, domain: string, options?: CreateShortCodeOptions): Promise<CreateShortCodeResponse>;
|
|
420
|
+
/**
|
|
421
|
+
* Get a short code by its code.
|
|
422
|
+
* @param {string} code The short code to retrieve. Example: 'code_686bed403c3991bd676bba4d'
|
|
423
|
+
* @returns {Promise<GetShortCodeResponse>} A promise that resolves to the short code details.
|
|
424
|
+
*/
|
|
425
|
+
getShortCode(code: string): Promise<GetShortCodeResponse>;
|
|
426
|
+
getShortCodes(titleSearch: string, tags?: string[], pageNumber?: number, pageSize?: number): Promise<GetShortCodesResponse>;
|
|
427
|
+
/**
|
|
428
|
+
* Delete a short code.
|
|
429
|
+
* @param {string} code The short code to delete. Example: 'code_686bed403c3991bd676bba4d'
|
|
430
|
+
* @returns {Promise<boolean>} A promise that resolves to true if the short code was deleted successfully, or false if it was not.
|
|
431
|
+
*/
|
|
432
|
+
deleteShortCode(code: string): Promise<boolean>;
|
|
433
|
+
}
|
|
434
|
+
|
|
318
435
|
type HyphenOptions = {
|
|
436
|
+
/**
|
|
437
|
+
* The public API key to use for the Hyphen service.
|
|
438
|
+
* This is used for public endpoints that do not require authentication.
|
|
439
|
+
*/
|
|
319
440
|
publicApiKey?: string;
|
|
441
|
+
/**
|
|
442
|
+
* The API key to use for the Hyphen service.
|
|
443
|
+
* This is used for authenticated endpoints that require an API key.
|
|
444
|
+
*/
|
|
320
445
|
apiKey?: string;
|
|
446
|
+
/**
|
|
447
|
+
* Whether to throw errors or not.
|
|
448
|
+
* If set to true, errors will be thrown instead of logged.
|
|
449
|
+
* @default false
|
|
450
|
+
*/
|
|
321
451
|
throwErrors?: boolean;
|
|
322
|
-
|
|
323
|
-
|
|
452
|
+
/**
|
|
453
|
+
* Options for the Toggle service.
|
|
454
|
+
* Excludes publicApiKey and throwErrors from ToggleOptions.
|
|
455
|
+
* @see ToggleOptions
|
|
456
|
+
* @default {Toggle}
|
|
457
|
+
*/
|
|
458
|
+
toggle?: Omit<ToggleOptions, 'publicApiKey' | 'throwErrors'>;
|
|
459
|
+
/**
|
|
460
|
+
* Options for the NetInfo service.
|
|
461
|
+
* Excludes apiKey and throwErrors from NetInfoOptions.
|
|
462
|
+
* @see NetInfoOptions
|
|
463
|
+
* @default {NetInfo}
|
|
464
|
+
*/
|
|
465
|
+
netInfo?: Omit<NetInfoOptions, 'apiKey' | 'throwErrors'>;
|
|
466
|
+
/**
|
|
467
|
+
* Options for the Link service.
|
|
468
|
+
* Excludes apiKey and throwErrors from LinkOptions.
|
|
469
|
+
* @see LinkOptions
|
|
470
|
+
* @default {Link}
|
|
471
|
+
*/
|
|
472
|
+
link?: Omit<LinkOptions, 'apiKey' | 'throwErrors'>;
|
|
324
473
|
} & HookifiedOptions;
|
|
325
474
|
declare class Hyphen extends Hookified {
|
|
326
475
|
private readonly _netInfo;
|
|
327
476
|
private readonly _toggle;
|
|
477
|
+
private readonly _link;
|
|
328
478
|
private _publicApiKey?;
|
|
329
479
|
private _apiKey?;
|
|
330
480
|
constructor(options?: HyphenOptions);
|
|
481
|
+
/**
|
|
482
|
+
* Get the NetInfo service instance.
|
|
483
|
+
* @returns {NetInfo} The NetInfo service instance.
|
|
484
|
+
*/
|
|
331
485
|
get netInfo(): NetInfo;
|
|
486
|
+
/**
|
|
487
|
+
* Get the Toggle service instance.
|
|
488
|
+
* @returns {Toggle} The Toggle service instance.
|
|
489
|
+
*/
|
|
332
490
|
get toggle(): Toggle;
|
|
491
|
+
/**
|
|
492
|
+
* Get the Link service instance.
|
|
493
|
+
* @returns {Link} The Link service instance.
|
|
494
|
+
*/
|
|
495
|
+
get link(): Link;
|
|
496
|
+
/**
|
|
497
|
+
* Get the public API key for the Hyphen service.
|
|
498
|
+
* This is used for public endpoints that do not require authentication.
|
|
499
|
+
* @returns {string | undefined} The public API key.
|
|
500
|
+
*/
|
|
333
501
|
get publicApiKey(): string | undefined;
|
|
502
|
+
/**
|
|
503
|
+
* Set the public API key for the Hyphen service. If set, this will also update the underlying services.
|
|
504
|
+
* This is used for public endpoints that do not require authentication such as the Toggle service.
|
|
505
|
+
* @param {string | undefined} value - The public API key to set.
|
|
506
|
+
*/
|
|
334
507
|
set publicApiKey(value: string | undefined);
|
|
508
|
+
/**
|
|
509
|
+
* Get the API key for the Hyphen service.
|
|
510
|
+
* This is used for authenticated endpoints that require an API key such as the NetInfo and Link services.
|
|
511
|
+
* @returns {string | undefined} The API key.
|
|
512
|
+
*/
|
|
335
513
|
get apiKey(): string | undefined;
|
|
514
|
+
/**
|
|
515
|
+
* Set the API key for the Hyphen service. If set, this will also update the underlying services.
|
|
516
|
+
* This is used for authenticated endpoints that require an API key such as the NetInfo and Link services.
|
|
517
|
+
* @param {string | undefined} value - The API key to set.
|
|
518
|
+
*/
|
|
336
519
|
set apiKey(value: string | undefined);
|
|
520
|
+
/**
|
|
521
|
+
* Get whether to throw errors or not.
|
|
522
|
+
* If set to true, errors will be thrown instead of logged.
|
|
523
|
+
* @returns {boolean} Whether to throw errors or not.
|
|
524
|
+
*/
|
|
337
525
|
get throwErrors(): boolean;
|
|
526
|
+
/**
|
|
527
|
+
* Set whether to throw errors or not. If set to true, errors will be thrown instead of logged.
|
|
528
|
+
* This will update the underlying services as well.
|
|
529
|
+
* @param {boolean} value - Whether to throw errors or not.
|
|
530
|
+
*/
|
|
338
531
|
set throwErrors(value: boolean);
|
|
339
532
|
}
|
|
340
533
|
|
package/dist/index.js
CHANGED
|
@@ -624,6 +624,191 @@ 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
|
+
/**
|
|
710
|
+
* Get the URI for a specific organization and code. This is used internally to construct the URI for the link service.
|
|
711
|
+
* @param {string} organizationId The ID of the organization.
|
|
712
|
+
* @param {string} code The code to include in the URI.
|
|
713
|
+
* @returns {string} The constructed URI.
|
|
714
|
+
*/
|
|
715
|
+
getUri(organizationId, code) {
|
|
716
|
+
if (!organizationId) {
|
|
717
|
+
throw new Error("Organization ID is required to get the URI.");
|
|
718
|
+
}
|
|
719
|
+
let url = this._uris[0].replace("{organizationId}", organizationId);
|
|
720
|
+
if (code) {
|
|
721
|
+
url = url.endsWith("/") ? `${url}${code}/` : `${url}/${code}/`;
|
|
722
|
+
}
|
|
723
|
+
return url;
|
|
724
|
+
}
|
|
725
|
+
async createShortCode(longUrl, domain, options) {
|
|
726
|
+
if (!this._organizationId) {
|
|
727
|
+
throw new Error("Organization ID is required to create a short code.");
|
|
728
|
+
}
|
|
729
|
+
const url = this.getUri(this._organizationId);
|
|
730
|
+
const body = {
|
|
731
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
732
|
+
long_url: longUrl,
|
|
733
|
+
domain,
|
|
734
|
+
code: options?.code,
|
|
735
|
+
title: options?.title,
|
|
736
|
+
tags: options?.tags
|
|
737
|
+
};
|
|
738
|
+
const headers = this.createHeaders(this._apiKey);
|
|
739
|
+
const response = await this.post(url, body, {
|
|
740
|
+
headers
|
|
741
|
+
});
|
|
742
|
+
if (response.status === 201) {
|
|
743
|
+
return response.data;
|
|
744
|
+
}
|
|
745
|
+
throw new Error(`Failed to create short code: ${response.statusText}`);
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* Get a short code by its code.
|
|
749
|
+
* @param {string} code The short code to retrieve. Example: 'code_686bed403c3991bd676bba4d'
|
|
750
|
+
* @returns {Promise<GetShortCodeResponse>} A promise that resolves to the short code details.
|
|
751
|
+
*/
|
|
752
|
+
async getShortCode(code) {
|
|
753
|
+
if (!this._organizationId) {
|
|
754
|
+
throw new Error("Organization ID is required to get a short code.");
|
|
755
|
+
}
|
|
756
|
+
const url = this.getUri(this._organizationId, code);
|
|
757
|
+
const headers = this.createHeaders(this._apiKey);
|
|
758
|
+
const response = await this.get(url, {
|
|
759
|
+
headers
|
|
760
|
+
});
|
|
761
|
+
if (response.status === 200) {
|
|
762
|
+
return response.data;
|
|
763
|
+
}
|
|
764
|
+
throw new Error(`Failed to get short code: ${response.statusText}`);
|
|
765
|
+
}
|
|
766
|
+
async getShortCodes(titleSearch, tags, pageNumber = 1, pageSize = 100) {
|
|
767
|
+
if (!this._organizationId) {
|
|
768
|
+
throw new Error("Organization ID is required to get short codes.");
|
|
769
|
+
}
|
|
770
|
+
const url = this.getUri(this._organizationId);
|
|
771
|
+
const headers = this.createHeaders(this._apiKey);
|
|
772
|
+
const parameters = {};
|
|
773
|
+
if (titleSearch) {
|
|
774
|
+
parameters.title = titleSearch;
|
|
775
|
+
}
|
|
776
|
+
if (tags && tags.length > 0) {
|
|
777
|
+
parameters.tags = tags.join(",");
|
|
778
|
+
}
|
|
779
|
+
parameters.pageNum = pageNumber.toString();
|
|
780
|
+
parameters.pageSize = pageSize.toString();
|
|
781
|
+
const response = await this.get(url, {
|
|
782
|
+
headers,
|
|
783
|
+
params: parameters
|
|
784
|
+
});
|
|
785
|
+
if (response.status === 200) {
|
|
786
|
+
return response.data;
|
|
787
|
+
}
|
|
788
|
+
throw new Error(`Failed to get short codes: ${response.statusText}`);
|
|
789
|
+
}
|
|
790
|
+
/**
|
|
791
|
+
* Delete a short code.
|
|
792
|
+
* @param {string} code The short code to delete. Example: 'code_686bed403c3991bd676bba4d'
|
|
793
|
+
* @returns {Promise<boolean>} A promise that resolves to true if the short code was deleted successfully, or false if it was not.
|
|
794
|
+
*/
|
|
795
|
+
async deleteShortCode(code) {
|
|
796
|
+
if (!this._organizationId) {
|
|
797
|
+
throw new Error("Organization ID is required to delete a short code.");
|
|
798
|
+
}
|
|
799
|
+
const url = this.getUri(this._organizationId, code);
|
|
800
|
+
const headers = this.createHeaders(this._apiKey);
|
|
801
|
+
delete headers["content-type"];
|
|
802
|
+
const response = await this.delete(url, {
|
|
803
|
+
headers
|
|
804
|
+
});
|
|
805
|
+
if (response.status === 204) {
|
|
806
|
+
return true;
|
|
807
|
+
}
|
|
808
|
+
throw new Error(`Failed to delete short code: ${response.statusText}`);
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
|
|
627
812
|
// src/hyphen.ts
|
|
628
813
|
var Hyphen = class extends Hookified3 {
|
|
629
814
|
static {
|
|
@@ -631,12 +816,14 @@ var Hyphen = class extends Hookified3 {
|
|
|
631
816
|
}
|
|
632
817
|
_netInfo;
|
|
633
818
|
_toggle;
|
|
819
|
+
_link;
|
|
634
820
|
_publicApiKey;
|
|
635
821
|
_apiKey;
|
|
636
822
|
constructor(options) {
|
|
637
823
|
super(options);
|
|
638
824
|
const toggleOptions = options?.toggle ?? {};
|
|
639
825
|
const netInfoOptions = options?.netInfo ?? {};
|
|
826
|
+
const linkOptions = options?.link ?? {};
|
|
640
827
|
if (options?.publicApiKey) {
|
|
641
828
|
this._publicApiKey = options.publicApiKey;
|
|
642
829
|
toggleOptions.publicApiKey = options.publicApiKey;
|
|
@@ -644,10 +831,12 @@ var Hyphen = class extends Hookified3 {
|
|
|
644
831
|
if (options?.apiKey) {
|
|
645
832
|
this._apiKey = options.apiKey;
|
|
646
833
|
netInfoOptions.apiKey = options.apiKey;
|
|
834
|
+
linkOptions.apiKey = options.apiKey;
|
|
647
835
|
}
|
|
648
836
|
if (options?.throwErrors !== void 0) {
|
|
649
837
|
toggleOptions.throwErrors = options.throwErrors;
|
|
650
838
|
netInfoOptions.throwErrors = options.throwErrors;
|
|
839
|
+
linkOptions.throwErrors = options.throwErrors;
|
|
651
840
|
}
|
|
652
841
|
this._netInfo = new NetInfo(netInfoOptions);
|
|
653
842
|
this._netInfo.on("error", (message, ...args) => this.emit("error", message, ...args));
|
|
@@ -657,33 +846,84 @@ var Hyphen = class extends Hookified3 {
|
|
|
657
846
|
this._toggle.on("error", (message, ...args) => this.emit("error", message, ...args));
|
|
658
847
|
this._toggle.on("info", (message, ...args) => this.emit("info", message, ...args));
|
|
659
848
|
this._toggle.on("warn", (message, ...args) => this.emit("warn", message, ...args));
|
|
849
|
+
this._link = new Link(linkOptions);
|
|
850
|
+
this._link.on("error", (message, ...args) => this.emit("error", message, ...args));
|
|
851
|
+
this._link.on("info", (message, ...args) => this.emit("info", message, ...args));
|
|
852
|
+
this._link.on("warn", (message, ...args) => this.emit("warn", message, ...args));
|
|
660
853
|
}
|
|
854
|
+
/**
|
|
855
|
+
* Get the NetInfo service instance.
|
|
856
|
+
* @returns {NetInfo} The NetInfo service instance.
|
|
857
|
+
*/
|
|
661
858
|
get netInfo() {
|
|
662
859
|
return this._netInfo;
|
|
663
860
|
}
|
|
861
|
+
/**
|
|
862
|
+
* Get the Toggle service instance.
|
|
863
|
+
* @returns {Toggle} The Toggle service instance.
|
|
864
|
+
*/
|
|
664
865
|
get toggle() {
|
|
665
866
|
return this._toggle;
|
|
666
867
|
}
|
|
868
|
+
/**
|
|
869
|
+
* Get the Link service instance.
|
|
870
|
+
* @returns {Link} The Link service instance.
|
|
871
|
+
*/
|
|
872
|
+
get link() {
|
|
873
|
+
return this._link;
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* Get the public API key for the Hyphen service.
|
|
877
|
+
* This is used for public endpoints that do not require authentication.
|
|
878
|
+
* @returns {string | undefined} The public API key.
|
|
879
|
+
*/
|
|
667
880
|
get publicApiKey() {
|
|
668
881
|
return this._publicApiKey;
|
|
669
882
|
}
|
|
883
|
+
/**
|
|
884
|
+
* Set the public API key for the Hyphen service. If set, this will also update the underlying services.
|
|
885
|
+
* This is used for public endpoints that do not require authentication such as the Toggle service.
|
|
886
|
+
* @param {string | undefined} value - The public API key to set.
|
|
887
|
+
*/
|
|
670
888
|
set publicApiKey(value) {
|
|
671
889
|
this._publicApiKey = value;
|
|
672
890
|
this._toggle.publicApiKey = value;
|
|
673
891
|
}
|
|
892
|
+
/**
|
|
893
|
+
* Get the API key for the Hyphen service.
|
|
894
|
+
* This is used for authenticated endpoints that require an API key such as the NetInfo and Link services.
|
|
895
|
+
* @returns {string | undefined} The API key.
|
|
896
|
+
*/
|
|
674
897
|
get apiKey() {
|
|
675
898
|
return this._apiKey;
|
|
676
899
|
}
|
|
900
|
+
/**
|
|
901
|
+
* Set the API key for the Hyphen service. If set, this will also update the underlying services.
|
|
902
|
+
* This is used for authenticated endpoints that require an API key such as the NetInfo and Link services.
|
|
903
|
+
* @param {string | undefined} value - The API key to set.
|
|
904
|
+
*/
|
|
677
905
|
set apiKey(value) {
|
|
678
906
|
this._apiKey = value;
|
|
679
907
|
this._netInfo.apiKey = value;
|
|
908
|
+
this._link.apiKey = value;
|
|
680
909
|
}
|
|
910
|
+
/**
|
|
911
|
+
* Get whether to throw errors or not.
|
|
912
|
+
* If set to true, errors will be thrown instead of logged.
|
|
913
|
+
* @returns {boolean} Whether to throw errors or not.
|
|
914
|
+
*/
|
|
681
915
|
get throwErrors() {
|
|
682
|
-
return this._netInfo.throwErrors && this._toggle.throwErrors;
|
|
916
|
+
return this._netInfo.throwErrors && this._toggle.throwErrors && this._link.throwErrors;
|
|
683
917
|
}
|
|
918
|
+
/**
|
|
919
|
+
* Set whether to throw errors or not. If set to true, errors will be thrown instead of logged.
|
|
920
|
+
* This will update the underlying services as well.
|
|
921
|
+
* @param {boolean} value - Whether to throw errors or not.
|
|
922
|
+
*/
|
|
684
923
|
set throwErrors(value) {
|
|
685
924
|
this._netInfo.throwErrors = value;
|
|
686
925
|
this._toggle.throwErrors = value;
|
|
926
|
+
this._link.throwErrors = value;
|
|
687
927
|
}
|
|
688
928
|
};
|
|
689
929
|
export {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyphen/sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "Hyphen SDK for Node.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -12,13 +12,6 @@
|
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
14
|
"types": "dist/index.d.ts",
|
|
15
|
-
"scripts": {
|
|
16
|
-
"test": "xo --fix && vitest run --coverage",
|
|
17
|
-
"test:ci": "xo && vitest run --coverage",
|
|
18
|
-
"build": "rimraf ./dist && tsup src/index.ts --format esm,cjs --dts --clean",
|
|
19
|
-
"clean": "rimraf ./dist pnpm-lock.yaml node_modules coverage",
|
|
20
|
-
"prepublishOnly": "rimraf ./dist && tsup src/index.ts --format esm,cjs --dts --clean"
|
|
21
|
-
},
|
|
22
15
|
"keywords": [
|
|
23
16
|
"hyphen",
|
|
24
17
|
"sdk",
|
|
@@ -44,6 +37,7 @@
|
|
|
44
37
|
"LICENSE"
|
|
45
38
|
],
|
|
46
39
|
"dependencies": {
|
|
40
|
+
"@faker-js/faker": "^9.9.0",
|
|
47
41
|
"@hyphen/openfeature-server-provider": "^1.0.7",
|
|
48
42
|
"@openfeature/server-sdk": "^1.18.0",
|
|
49
43
|
"axios": "^1.10.0",
|
|
@@ -51,5 +45,11 @@
|
|
|
51
45
|
"dotenv": "^17.0.1",
|
|
52
46
|
"hookified": "^1.10.0",
|
|
53
47
|
"pino": "^9.7.0"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"test": "xo --fix && vitest run --coverage",
|
|
51
|
+
"test:ci": "xo && vitest run --coverage",
|
|
52
|
+
"build": "rimraf ./dist && tsup src/index.ts --format esm,cjs --dts --clean",
|
|
53
|
+
"clean": "rimraf ./dist pnpm-lock.yaml node_modules coverage"
|
|
54
54
|
}
|
|
55
|
-
}
|
|
55
|
+
}
|