@xylabs/api 5.0.83 → 5.0.86
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 +63 -75
- package/dist/neutral/ApiClient.d.ts +1 -0
- package/dist/neutral/ApiClient.d.ts.map +1 -1
- package/dist/neutral/ApiConfig.d.ts +1 -0
- package/dist/neutral/ApiConfig.d.ts.map +1 -1
- package/dist/neutral/ApiEndpoint.d.ts +4 -0
- package/dist/neutral/ApiEndpoint.d.ts.map +1 -1
- package/dist/neutral/ApiStage.d.ts +2 -0
- package/dist/neutral/ApiStage.d.ts.map +1 -1
- package/dist/neutral/getApiStage.d.ts +5 -0
- package/dist/neutral/getApiStage.d.ts.map +1 -1
- package/dist/neutral/index.mjs.map +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
|
|
16
16
|
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
|
|
17
17
|
|
|
18
|
+
|
|
19
|
+
|
|
18
20
|
## Reference
|
|
19
21
|
|
|
20
22
|
**@xylabs/api**
|
|
@@ -23,24 +25,34 @@ Base functionality used throughout XY Labs TypeScript/JavaScript libraries
|
|
|
23
25
|
|
|
24
26
|
## Classes
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
| Class | Description |
|
|
29
|
+
| ------ | ------ |
|
|
30
|
+
| [ApiClient](#classes/ApiClient) | Abstract base class for API clients that provides stage and token configuration. |
|
|
31
|
+
| [ApiEndpoint](#classes/ApiEndpoint) | Generic REST API endpoint wrapper that supports fetching and inserting typed data. |
|
|
28
32
|
|
|
29
33
|
## Interfaces
|
|
30
34
|
|
|
31
|
-
|
|
35
|
+
| Interface | Description |
|
|
36
|
+
| ------ | ------ |
|
|
37
|
+
| [ApiConfig](#interfaces/ApiConfig) | Configuration for connecting to an API, including domain, authentication, and user identification. |
|
|
32
38
|
|
|
33
39
|
## Type Aliases
|
|
34
40
|
|
|
35
|
-
|
|
41
|
+
| Type Alias | Description |
|
|
42
|
+
| ------ | ------ |
|
|
43
|
+
| [ApiStage](#type-aliases/ApiStage) | A valid API stage value ('prod', 'beta', or 'local'). |
|
|
36
44
|
|
|
37
45
|
## Variables
|
|
38
46
|
|
|
39
|
-
|
|
47
|
+
| Variable | Description |
|
|
48
|
+
| ------ | ------ |
|
|
49
|
+
| [ApiStage](#variables/ApiStage) | Deployment stage identifiers for API environments. |
|
|
40
50
|
|
|
41
51
|
## Functions
|
|
42
52
|
|
|
43
|
-
|
|
53
|
+
| Function | Description |
|
|
54
|
+
| ------ | ------ |
|
|
55
|
+
| [getApiStage](#functions/getApiStage) | Determines the API stage based on the hostname. |
|
|
44
56
|
|
|
45
57
|
### classes
|
|
46
58
|
|
|
@@ -50,23 +62,22 @@ Base functionality used throughout XY Labs TypeScript/JavaScript libraries
|
|
|
50
62
|
|
|
51
63
|
***
|
|
52
64
|
|
|
65
|
+
Abstract base class for API clients that provides stage and token configuration.
|
|
66
|
+
|
|
53
67
|
## Constructors
|
|
54
68
|
|
|
55
69
|
### Constructor
|
|
56
70
|
|
|
57
71
|
```ts
|
|
58
|
-
new ApiClient(token
|
|
72
|
+
new ApiClient(token?: string | null, stage?: ApiStage): ApiClient;
|
|
59
73
|
```
|
|
60
74
|
|
|
61
75
|
### Parameters
|
|
62
76
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
`string`
|
|
66
|
-
|
|
67
|
-
#### stage?
|
|
68
|
-
|
|
69
|
-
[`ApiStage`](#../type-aliases/ApiStage)
|
|
77
|
+
| Parameter | Type |
|
|
78
|
+
| ------ | ------ |
|
|
79
|
+
| `token?` | `string` \| `null` |
|
|
80
|
+
| `stage?` | [`ApiStage`](#../type-aliases/ApiStage) |
|
|
70
81
|
|
|
71
82
|
### Returns
|
|
72
83
|
|
|
@@ -74,19 +85,10 @@ new ApiClient(token?, stage?): ApiClient;
|
|
|
74
85
|
|
|
75
86
|
## Properties
|
|
76
87
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
protected
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
***
|
|
84
|
-
|
|
85
|
-
### token?
|
|
86
|
-
|
|
87
|
-
```ts
|
|
88
|
-
protected optional token: string | null;
|
|
89
|
-
```
|
|
88
|
+
| Property | Modifier | Type |
|
|
89
|
+
| ------ | ------ | ------ |
|
|
90
|
+
| <a id="stage"></a> `stage?` | `protected` | [`ApiStage`](#../type-aliases/ApiStage) |
|
|
91
|
+
| <a id="token"></a> `token?` | `protected` | `string` \| `null` |
|
|
90
92
|
|
|
91
93
|
## Methods
|
|
92
94
|
|
|
@@ -106,29 +108,28 @@ abstract endPoint(): string;
|
|
|
106
108
|
|
|
107
109
|
***
|
|
108
110
|
|
|
109
|
-
|
|
111
|
+
Generic REST API endpoint wrapper that supports fetching and inserting typed data.
|
|
110
112
|
|
|
111
|
-
|
|
113
|
+
## Type Parameters
|
|
112
114
|
|
|
113
|
-
|
|
115
|
+
| Type Parameter | Description |
|
|
116
|
+
| ------ | ------ |
|
|
117
|
+
| `T` | The type of data returned by the endpoint |
|
|
114
118
|
|
|
115
119
|
## Constructors
|
|
116
120
|
|
|
117
121
|
### Constructor
|
|
118
122
|
|
|
119
123
|
```ts
|
|
120
|
-
new ApiEndpoint<T>(config, path): ApiEndpoint<T>;
|
|
124
|
+
new ApiEndpoint<T>(config: ApiConfig, path: string): ApiEndpoint<T>;
|
|
121
125
|
```
|
|
122
126
|
|
|
123
127
|
### Parameters
|
|
124
128
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
[`ApiConfig`](#../interfaces/ApiConfig)
|
|
128
|
-
|
|
129
|
-
#### path
|
|
130
|
-
|
|
131
|
-
`string`
|
|
129
|
+
| Parameter | Type |
|
|
130
|
+
| ------ | ------ |
|
|
131
|
+
| `config` | [`ApiConfig`](#../interfaces/ApiConfig) |
|
|
132
|
+
| `path` | `string` |
|
|
132
133
|
|
|
133
134
|
### Returns
|
|
134
135
|
|
|
@@ -177,14 +178,14 @@ get(): Promise<T | NonNullable<T>>;
|
|
|
177
178
|
### insert()
|
|
178
179
|
|
|
179
180
|
```ts
|
|
180
|
-
insert(value): Promise<T>;
|
|
181
|
+
insert(value: T): Promise<T>;
|
|
181
182
|
```
|
|
182
183
|
|
|
183
184
|
### Parameters
|
|
184
185
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
`T`
|
|
186
|
+
| Parameter | Type |
|
|
187
|
+
| ------ | ------ |
|
|
188
|
+
| `value` | `T` |
|
|
188
189
|
|
|
189
190
|
### Returns
|
|
190
191
|
|
|
@@ -199,19 +200,23 @@ insert(value): Promise<T>;
|
|
|
199
200
|
***
|
|
200
201
|
|
|
201
202
|
```ts
|
|
202
|
-
function getApiStage(hostname): "beta" | "local" | "prod";
|
|
203
|
+
function getApiStage(hostname: string): "beta" | "local" | "prod";
|
|
203
204
|
```
|
|
204
205
|
|
|
205
|
-
|
|
206
|
+
Determines the API stage based on the hostname.
|
|
206
207
|
|
|
207
|
-
|
|
208
|
+
## Parameters
|
|
208
209
|
|
|
209
|
-
|
|
210
|
+
| Parameter | Type | Description |
|
|
211
|
+
| ------ | ------ | ------ |
|
|
212
|
+
| `hostname` | `string` | The hostname to evaluate |
|
|
210
213
|
|
|
211
214
|
## Returns
|
|
212
215
|
|
|
213
216
|
`"beta"` \| `"local"` \| `"prod"`
|
|
214
217
|
|
|
218
|
+
The corresponding ApiStage (Local, Beta, or Prod)
|
|
219
|
+
|
|
215
220
|
### interfaces
|
|
216
221
|
|
|
217
222
|
### <a id="ApiConfig"></a>ApiConfig
|
|
@@ -220,37 +225,16 @@ function getApiStage(hostname): "beta" | "local" | "prod";
|
|
|
220
225
|
|
|
221
226
|
***
|
|
222
227
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
### apiDomain
|
|
228
|
+
Configuration for connecting to an API, including domain, authentication, and user identification.
|
|
226
229
|
|
|
227
|
-
|
|
228
|
-
apiDomain: string;
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
***
|
|
232
|
-
|
|
233
|
-
### apiKey?
|
|
234
|
-
|
|
235
|
-
```ts
|
|
236
|
-
optional apiKey: string;
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
***
|
|
240
|
-
|
|
241
|
-
### jwtToken?
|
|
242
|
-
|
|
243
|
-
```ts
|
|
244
|
-
optional jwtToken: string;
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
***
|
|
248
|
-
|
|
249
|
-
### userid?
|
|
230
|
+
## Properties
|
|
250
231
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
232
|
+
| Property | Type |
|
|
233
|
+
| ------ | ------ |
|
|
234
|
+
| <a id="apidomain"></a> `apiDomain` | `string` |
|
|
235
|
+
| <a id="apikey"></a> `apiKey?` | `string` |
|
|
236
|
+
| <a id="jwttoken"></a> `jwtToken?` | `string` |
|
|
237
|
+
| <a id="userid"></a> `userid?` | `string` |
|
|
254
238
|
|
|
255
239
|
### type-aliases
|
|
256
240
|
|
|
@@ -264,6 +248,8 @@ optional userid: string;
|
|
|
264
248
|
type ApiStage = EnumValue<typeof ApiStage>;
|
|
265
249
|
```
|
|
266
250
|
|
|
251
|
+
A valid API stage value ('prod', 'beta', or 'local').
|
|
252
|
+
|
|
267
253
|
### variables
|
|
268
254
|
|
|
269
255
|
### <a id="ApiStage"></a>ApiStage
|
|
@@ -280,6 +266,8 @@ const ApiStage: Enum<{
|
|
|
280
266
|
}>;
|
|
281
267
|
```
|
|
282
268
|
|
|
269
|
+
Deployment stage identifiers for API environments.
|
|
270
|
+
|
|
283
271
|
|
|
284
272
|
Part of [sdk-js](https://www.npmjs.com/package/@xyo-network/sdk-js)
|
|
285
273
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApiClient.d.ts","sourceRoot":"","sources":["../../src/ApiClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAExC,uBAAe,SAAS;IACtB,SAAS,CAAC,KAAK,CAAC,EAAE,QAAQ,CAAA;IAC1B,SAAS,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;gBAG7B,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,EACrB,KAAK,CAAC,EAAE,QAAQ;IAMlB,QAAQ,CAAC,QAAQ,IAAI,MAAM;CAC5B;AAED,OAAO,EAAE,SAAS,EAAE,CAAA"}
|
|
1
|
+
{"version":3,"file":"ApiClient.d.ts","sourceRoot":"","sources":["../../src/ApiClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAExC,uFAAuF;AACvF,uBAAe,SAAS;IACtB,SAAS,CAAC,KAAK,CAAC,EAAE,QAAQ,CAAA;IAC1B,SAAS,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;gBAG7B,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,EACrB,KAAK,CAAC,EAAE,QAAQ;IAMlB,QAAQ,CAAC,QAAQ,IAAI,MAAM;CAC5B;AAED,OAAO,EAAE,SAAS,EAAE,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApiConfig.d.ts","sourceRoot":"","sources":["../../src/ApiConfig.ts"],"names":[],"mappings":"AAAA,UAAU,SAAS;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED,YAAY,EAAE,SAAS,EAAE,CAAA"}
|
|
1
|
+
{"version":3,"file":"ApiConfig.d.ts","sourceRoot":"","sources":["../../src/ApiConfig.ts"],"names":[],"mappings":"AAAA,yGAAyG;AACzG,UAAU,SAAS;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED,YAAY,EAAE,SAAS,EAAE,CAAA"}
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import type { ApiConfig } from './ApiConfig.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Generic REST API endpoint wrapper that supports fetching and inserting typed data.
|
|
4
|
+
* @typeParam T - The type of data returned by the endpoint
|
|
5
|
+
*/
|
|
2
6
|
declare class ApiEndpoint<T> {
|
|
3
7
|
private _value?;
|
|
4
8
|
private config;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApiEndpoint.d.ts","sourceRoot":"","sources":["../../src/ApiEndpoint.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE/C,cAAM,WAAW,CAAC,CAAC;IACjB,OAAO,CAAC,MAAM,CAAC,CAAG;IAClB,OAAO,CAAC,MAAM,CAAW;IACzB,OAAO,CAAC,IAAI,CAAQ;gBAER,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM;IAK3C,IAAI,KAAK,kBAER;IAED,OAAO,KAAK,OAAO,GAElB;IAED,OAAO,KAAK,GAAG,GAEd;IAEK,KAAK;IAUL,GAAG;IAIH,MAAM,CAAC,KAAK,EAAE,CAAC;CAStB;AAED,OAAO,EAAE,WAAW,EAAE,CAAA"}
|
|
1
|
+
{"version":3,"file":"ApiEndpoint.d.ts","sourceRoot":"","sources":["../../src/ApiEndpoint.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE/C;;;GAGG;AACH,cAAM,WAAW,CAAC,CAAC;IACjB,OAAO,CAAC,MAAM,CAAC,CAAG;IAClB,OAAO,CAAC,MAAM,CAAW;IACzB,OAAO,CAAC,IAAI,CAAQ;gBAER,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM;IAK3C,IAAI,KAAK,kBAER;IAED,OAAO,KAAK,OAAO,GAElB;IAED,OAAO,KAAK,GAAG,GAEd;IAEK,KAAK;IAUL,GAAG;IAIH,MAAM,CAAC,KAAK,EAAE,CAAC;CAStB;AAED,OAAO,EAAE,WAAW,EAAE,CAAA"}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { EnumValue } from '@xylabs/enum';
|
|
2
2
|
import { Enum } from '@xylabs/enum';
|
|
3
|
+
/** Deployment stage identifiers for API environments. */
|
|
3
4
|
export declare const ApiStage: Enum<{
|
|
4
5
|
Beta: "beta";
|
|
5
6
|
Local: "local";
|
|
6
7
|
Prod: "prod";
|
|
7
8
|
}>;
|
|
9
|
+
/** A valid API stage value ('prod', 'beta', or 'local'). */
|
|
8
10
|
export type ApiStage = EnumValue<typeof ApiStage>;
|
|
9
11
|
//# sourceMappingURL=ApiStage.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApiStage.d.ts","sourceRoot":"","sources":["../../src/ApiStage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AAEnC,eAAO,MAAM,QAAQ;;;;EAInB,CAAA;AAEF,MAAM,MAAM,QAAQ,GAAG,SAAS,CAAC,OAAO,QAAQ,CAAC,CAAA"}
|
|
1
|
+
{"version":3,"file":"ApiStage.d.ts","sourceRoot":"","sources":["../../src/ApiStage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AAEnC,yDAAyD;AACzD,eAAO,MAAM,QAAQ;;;;EAInB,CAAA;AAEF,4DAA4D;AAC5D,MAAM,MAAM,QAAQ,GAAG,SAAS,CAAC,OAAO,QAAQ,CAAC,CAAA"}
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Determines the API stage based on the hostname.
|
|
3
|
+
* @param hostname - The hostname to evaluate
|
|
4
|
+
* @returns The corresponding ApiStage (Local, Beta, or Prod)
|
|
5
|
+
*/
|
|
1
6
|
declare const getApiStage: (hostname: string) => "beta" | "local" | "prod";
|
|
2
7
|
export { getApiStage };
|
|
3
8
|
//# sourceMappingURL=getApiStage.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getApiStage.d.ts","sourceRoot":"","sources":["../../src/getApiStage.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,WAAW,GAAI,UAAU,MAAM,8BAQpC,CAAA;AAED,OAAO,EAAE,WAAW,EAAE,CAAA"}
|
|
1
|
+
{"version":3,"file":"getApiStage.d.ts","sourceRoot":"","sources":["../../src/getApiStage.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,QAAA,MAAM,WAAW,GAAI,UAAU,MAAM,8BAQpC,CAAA;AAED,OAAO,EAAE,WAAW,EAAE,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ApiStage.ts","../../src/ApiClient.ts","../../src/ApiEndpoint.ts","../../src/getApiStage.ts"],"sourcesContent":["import type { EnumValue } from '@xylabs/enum'\nimport { Enum } from '@xylabs/enum'\n\nexport const ApiStage = Enum({\n Beta: 'beta',\n Local: 'local',\n Prod: 'prod',\n})\n\nexport type ApiStage = EnumValue<typeof ApiStage>\n","import { ApiStage } from './ApiStage.ts'\n\nabstract class ApiClient {\n protected stage?: ApiStage\n protected token?: string | null\n\n constructor(\n token?: string | null,\n stage?: ApiStage,\n ) {\n this.stage = stage ?? ApiStage.Prod\n this.token = token\n }\n\n abstract endPoint(): string\n}\n\nexport { ApiClient }\n","import { isUndefined } from '@xylabs/typeof'\nimport axios from 'axios'\n\nimport type { ApiConfig } from './ApiConfig.ts'\n\nclass ApiEndpoint<T> {\n private _value?: T\n private config: ApiConfig\n private path: string\n\n constructor(config: ApiConfig, path: string) {\n this.config = config\n this.path = path\n }\n\n get value() {\n return this._value\n }\n\n private get headers() {\n return isUndefined(this.config.jwtToken) ? undefined : { Authorization: this.config.jwtToken }\n }\n\n private get url() {\n return `${this.config.apiDomain}/${this.path}`\n }\n\n async fetch() {\n const response = await axios.get<T>(this.url, { headers: this.headers })\n if (response.status === 200) {\n this._value = response.data\n } else {\n throw new Error('Unexpected Status Code')\n }\n return this._value\n }\n\n async get() {\n return this._value ?? (await this.fetch())\n }\n\n async insert(value: T) {\n const response = await axios.post<T>(this.url, value, { headers: this.headers })\n if (response.status === 200) {\n this._value = response.data\n } else {\n throw new Error('Unexpected Status Code')\n }\n return this._value\n }\n}\n\nexport { ApiEndpoint }\n","import { ApiStage } from './ApiStage.ts'\n\nconst getApiStage = (hostname: string) => {\n if (hostname.startsWith('localhost')) {\n return ApiStage.Local\n } else if (hostname.startsWith('beta.')) {\n return ApiStage.Beta\n } else {\n return ApiStage.Prod\n }\n}\n\nexport { getApiStage }\n"],"mappings":";AACA,SAAS,YAAY;
|
|
1
|
+
{"version":3,"sources":["../../src/ApiStage.ts","../../src/ApiClient.ts","../../src/ApiEndpoint.ts","../../src/getApiStage.ts"],"sourcesContent":["import type { EnumValue } from '@xylabs/enum'\nimport { Enum } from '@xylabs/enum'\n\n/** Deployment stage identifiers for API environments. */\nexport const ApiStage = Enum({\n Beta: 'beta',\n Local: 'local',\n Prod: 'prod',\n})\n\n/** A valid API stage value ('prod', 'beta', or 'local'). */\nexport type ApiStage = EnumValue<typeof ApiStage>\n","import { ApiStage } from './ApiStage.ts'\n\n/** Abstract base class for API clients that provides stage and token configuration. */\nabstract class ApiClient {\n protected stage?: ApiStage\n protected token?: string | null\n\n constructor(\n token?: string | null,\n stage?: ApiStage,\n ) {\n this.stage = stage ?? ApiStage.Prod\n this.token = token\n }\n\n abstract endPoint(): string\n}\n\nexport { ApiClient }\n","import { isUndefined } from '@xylabs/typeof'\nimport axios from 'axios'\n\nimport type { ApiConfig } from './ApiConfig.ts'\n\n/**\n * Generic REST API endpoint wrapper that supports fetching and inserting typed data.\n * @typeParam T - The type of data returned by the endpoint\n */\nclass ApiEndpoint<T> {\n private _value?: T\n private config: ApiConfig\n private path: string\n\n constructor(config: ApiConfig, path: string) {\n this.config = config\n this.path = path\n }\n\n get value() {\n return this._value\n }\n\n private get headers() {\n return isUndefined(this.config.jwtToken) ? undefined : { Authorization: this.config.jwtToken }\n }\n\n private get url() {\n return `${this.config.apiDomain}/${this.path}`\n }\n\n async fetch() {\n const response = await axios.get<T>(this.url, { headers: this.headers })\n if (response.status === 200) {\n this._value = response.data\n } else {\n throw new Error('Unexpected Status Code')\n }\n return this._value\n }\n\n async get() {\n return this._value ?? (await this.fetch())\n }\n\n async insert(value: T) {\n const response = await axios.post<T>(this.url, value, { headers: this.headers })\n if (response.status === 200) {\n this._value = response.data\n } else {\n throw new Error('Unexpected Status Code')\n }\n return this._value\n }\n}\n\nexport { ApiEndpoint }\n","import { ApiStage } from './ApiStage.ts'\n\n/**\n * Determines the API stage based on the hostname.\n * @param hostname - The hostname to evaluate\n * @returns The corresponding ApiStage (Local, Beta, or Prod)\n */\nconst getApiStage = (hostname: string) => {\n if (hostname.startsWith('localhost')) {\n return ApiStage.Local\n } else if (hostname.startsWith('beta.')) {\n return ApiStage.Beta\n } else {\n return ApiStage.Prod\n }\n}\n\nexport { getApiStage }\n"],"mappings":";AACA,SAAS,YAAY;AAGd,IAAM,WAAW,KAAK;AAAA,EAC3B,MAAM;AAAA,EACN,OAAO;AAAA,EACP,MAAM;AACR,CAAC;;;ACLD,IAAe,YAAf,MAAyB;AAAA,EACb;AAAA,EACA;AAAA,EAEV,YACE,OACA,OACA;AACA,SAAK,QAAQ,SAAS,SAAS;AAC/B,SAAK,QAAQ;AAAA,EACf;AAGF;;;AChBA,SAAS,mBAAmB;AAC5B,OAAO,WAAW;AAQlB,IAAM,cAAN,MAAqB;AAAA,EACX;AAAA,EACA;AAAA,EACA;AAAA,EAER,YAAY,QAAmB,MAAc;AAC3C,SAAK,SAAS;AACd,SAAK,OAAO;AAAA,EACd;AAAA,EAEA,IAAI,QAAQ;AACV,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAY,UAAU;AACpB,WAAO,YAAY,KAAK,OAAO,QAAQ,IAAI,SAAY,EAAE,eAAe,KAAK,OAAO,SAAS;AAAA,EAC/F;AAAA,EAEA,IAAY,MAAM;AAChB,WAAO,GAAG,KAAK,OAAO,SAAS,IAAI,KAAK,IAAI;AAAA,EAC9C;AAAA,EAEA,MAAM,QAAQ;AACZ,UAAM,WAAW,MAAM,MAAM,IAAO,KAAK,KAAK,EAAE,SAAS,KAAK,QAAQ,CAAC;AACvE,QAAI,SAAS,WAAW,KAAK;AAC3B,WAAK,SAAS,SAAS;AAAA,IACzB,OAAO;AACL,YAAM,IAAI,MAAM,wBAAwB;AAAA,IAC1C;AACA,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,MAAM,MAAM;AACV,WAAO,KAAK,UAAW,MAAM,KAAK,MAAM;AAAA,EAC1C;AAAA,EAEA,MAAM,OAAO,OAAU;AACrB,UAAM,WAAW,MAAM,MAAM,KAAQ,KAAK,KAAK,OAAO,EAAE,SAAS,KAAK,QAAQ,CAAC;AAC/E,QAAI,SAAS,WAAW,KAAK;AAC3B,WAAK,SAAS,SAAS;AAAA,IACzB,OAAO;AACL,YAAM,IAAI,MAAM,wBAAwB;AAAA,IAC1C;AACA,WAAO,KAAK;AAAA,EACd;AACF;;;AC/CA,IAAM,cAAc,CAAC,aAAqB;AACxC,MAAI,SAAS,WAAW,WAAW,GAAG;AACpC,WAAO,SAAS;AAAA,EAClB,WAAW,SAAS,WAAW,OAAO,GAAG;AACvC,WAAO,SAAS;AAAA,EAClB,OAAO;AACL,WAAO,SAAS;AAAA,EAClB;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/api",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.86",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"xylabs",
|
|
@@ -41,12 +41,12 @@
|
|
|
41
41
|
"!**/*.test.*"
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@xylabs/enum": "~5.0.
|
|
45
|
-
"@xylabs/typeof": "~5.0.
|
|
44
|
+
"@xylabs/enum": "~5.0.86",
|
|
45
|
+
"@xylabs/typeof": "~5.0.86"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@xylabs/ts-scripts-yarn3": "~7.4.
|
|
49
|
-
"@xylabs/tsconfig": "~7.4.
|
|
48
|
+
"@xylabs/ts-scripts-yarn3": "~7.4.16",
|
|
49
|
+
"@xylabs/tsconfig": "~7.4.16",
|
|
50
50
|
"axios": "^1.13.6",
|
|
51
51
|
"typescript": "~5.9.3",
|
|
52
52
|
"vitest": "~4.0.18"
|