@hyphen/sdk 1.10.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 +42 -1
- package/dist/index.cjs +61 -3
- package/dist/index.d.cts +21 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +61 -3
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -24,6 +24,10 @@ The Hyphen Node.js SDK is a JavaScript library that allows developers to easily
|
|
|
24
24
|
- [Loading Environment Variables](#loading-environment-variables)
|
|
25
25
|
- [Net Info - Geo Information Service](#net-info---geo-information-service)
|
|
26
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)
|
|
27
31
|
- [Contributing](#contributing)
|
|
28
32
|
- [Testing Your Changes](#testing-your-changes)
|
|
29
33
|
- [License and Copyright](#license-and-copyright)
|
|
@@ -653,7 +657,14 @@ You can also set the API key using the `HYPHEN_API_KEY` environment variable. Th
|
|
|
653
657
|
|
|
654
658
|
# Link - Short Code Service
|
|
655
659
|
|
|
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.
|
|
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:
|
|
657
668
|
|
|
658
669
|
```javascript
|
|
659
670
|
import { Link } from '@hyphen/sdk';
|
|
@@ -670,6 +681,36 @@ const response = await link.createShortCode(longUrl, domain, options);
|
|
|
670
681
|
console.log('Short Code Response:', response);
|
|
671
682
|
```
|
|
672
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
|
|
673
714
|
if you want to delete a short code you can do it like this:
|
|
674
715
|
|
|
675
716
|
```javascript
|
package/dist/index.cjs
CHANGED
|
@@ -743,11 +743,27 @@ var Link = class extends BaseService {
|
|
|
743
743
|
this._apiKey = apiKey;
|
|
744
744
|
}
|
|
745
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
|
+
}
|
|
746
762
|
async createShortCode(longUrl, domain, options) {
|
|
747
763
|
if (!this._organizationId) {
|
|
748
764
|
throw new Error("Organization ID is required to create a short code.");
|
|
749
765
|
}
|
|
750
|
-
const url = this.
|
|
766
|
+
const url = this.getUri(this._organizationId);
|
|
751
767
|
const body = {
|
|
752
768
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
753
769
|
long_url: longUrl,
|
|
@@ -766,6 +782,49 @@ var Link = class extends BaseService {
|
|
|
766
782
|
throw new Error(`Failed to create short code: ${response.statusText}`);
|
|
767
783
|
}
|
|
768
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
|
+
/**
|
|
769
828
|
* Delete a short code.
|
|
770
829
|
* @param {string} code The short code to delete. Example: 'code_686bed403c3991bd676bba4d'
|
|
771
830
|
* @returns {Promise<boolean>} A promise that resolves to true if the short code was deleted successfully, or false if it was not.
|
|
@@ -774,8 +833,7 @@ var Link = class extends BaseService {
|
|
|
774
833
|
if (!this._organizationId) {
|
|
775
834
|
throw new Error("Organization ID is required to delete a short code.");
|
|
776
835
|
}
|
|
777
|
-
|
|
778
|
-
url = url.endsWith("/") ? `${url}${code}/` : `${url}/${code}/`;
|
|
836
|
+
const url = this.getUri(this._organizationId, code);
|
|
779
837
|
const headers = this.createHeaders(this._apiKey);
|
|
780
838
|
delete headers["content-type"];
|
|
781
839
|
const response = await this.delete(url, {
|
package/dist/index.d.cts
CHANGED
|
@@ -345,6 +345,13 @@ type CreateShortCodeResponse = {
|
|
|
345
345
|
name: string;
|
|
346
346
|
};
|
|
347
347
|
};
|
|
348
|
+
type GetShortCodesResponse = {
|
|
349
|
+
total: number;
|
|
350
|
+
pageNum: number;
|
|
351
|
+
pageSize: number;
|
|
352
|
+
data: GetShortCodeResponse[];
|
|
353
|
+
};
|
|
354
|
+
type GetShortCodeResponse = CreateShortCodeResponse;
|
|
348
355
|
type LinkOptions = {
|
|
349
356
|
/**
|
|
350
357
|
* The URIs to access the link service.
|
|
@@ -402,7 +409,21 @@ declare class Link extends BaseService {
|
|
|
402
409
|
* @param {string} apiKey
|
|
403
410
|
*/
|
|
404
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;
|
|
405
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>;
|
|
406
427
|
/**
|
|
407
428
|
* Delete a short code.
|
|
408
429
|
* @param {string} code The short code to delete. Example: 'code_686bed403c3991bd676bba4d'
|
package/dist/index.d.ts
CHANGED
|
@@ -345,6 +345,13 @@ type CreateShortCodeResponse = {
|
|
|
345
345
|
name: string;
|
|
346
346
|
};
|
|
347
347
|
};
|
|
348
|
+
type GetShortCodesResponse = {
|
|
349
|
+
total: number;
|
|
350
|
+
pageNum: number;
|
|
351
|
+
pageSize: number;
|
|
352
|
+
data: GetShortCodeResponse[];
|
|
353
|
+
};
|
|
354
|
+
type GetShortCodeResponse = CreateShortCodeResponse;
|
|
348
355
|
type LinkOptions = {
|
|
349
356
|
/**
|
|
350
357
|
* The URIs to access the link service.
|
|
@@ -402,7 +409,21 @@ declare class Link extends BaseService {
|
|
|
402
409
|
* @param {string} apiKey
|
|
403
410
|
*/
|
|
404
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;
|
|
405
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>;
|
|
406
427
|
/**
|
|
407
428
|
* Delete a short code.
|
|
408
429
|
* @param {string} code The short code to delete. Example: 'code_686bed403c3991bd676bba4d'
|
package/dist/index.js
CHANGED
|
@@ -706,11 +706,27 @@ var Link = class extends BaseService {
|
|
|
706
706
|
this._apiKey = apiKey;
|
|
707
707
|
}
|
|
708
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
|
+
}
|
|
709
725
|
async createShortCode(longUrl, domain, options) {
|
|
710
726
|
if (!this._organizationId) {
|
|
711
727
|
throw new Error("Organization ID is required to create a short code.");
|
|
712
728
|
}
|
|
713
|
-
const url = this.
|
|
729
|
+
const url = this.getUri(this._organizationId);
|
|
714
730
|
const body = {
|
|
715
731
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
716
732
|
long_url: longUrl,
|
|
@@ -729,6 +745,49 @@ var Link = class extends BaseService {
|
|
|
729
745
|
throw new Error(`Failed to create short code: ${response.statusText}`);
|
|
730
746
|
}
|
|
731
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
|
+
/**
|
|
732
791
|
* Delete a short code.
|
|
733
792
|
* @param {string} code The short code to delete. Example: 'code_686bed403c3991bd676bba4d'
|
|
734
793
|
* @returns {Promise<boolean>} A promise that resolves to true if the short code was deleted successfully, or false if it was not.
|
|
@@ -737,8 +796,7 @@ var Link = class extends BaseService {
|
|
|
737
796
|
if (!this._organizationId) {
|
|
738
797
|
throw new Error("Organization ID is required to delete a short code.");
|
|
739
798
|
}
|
|
740
|
-
|
|
741
|
-
url = url.endsWith("/") ? `${url}${code}/` : `${url}/${code}/`;
|
|
799
|
+
const url = this.getUri(this._organizationId, code);
|
|
742
800
|
const headers = this.createHeaders(this._apiKey);
|
|
743
801
|
delete headers["content-type"];
|
|
744
802
|
const response = await this.delete(url, {
|
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
|
+
}
|