@oblax/core 1.0.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/dist/errors.d.ts +17 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +27 -0
- package/dist/errors.js.map +1 -0
- package/dist/http.d.ts +28 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +131 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/oblax.d.ts +26 -0
- package/dist/oblax.d.ts.map +1 -0
- package/dist/oblax.js +86 -0
- package/dist/oblax.js.map +1 -0
- package/dist/types.d.ts +200 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +47 -0
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Base error for all Oblax SDK errors. */
|
|
2
|
+
export declare class OblaxError extends Error {
|
|
3
|
+
constructor(message: string);
|
|
4
|
+
}
|
|
5
|
+
/** Thrown when the backend returns a non-2xx HTTP response. */
|
|
6
|
+
export declare class OblaxHttpError extends OblaxError {
|
|
7
|
+
/** HTTP status code returned by the server. */
|
|
8
|
+
readonly status: number;
|
|
9
|
+
/** Raw response body, if parseable as JSON. */
|
|
10
|
+
readonly body: unknown;
|
|
11
|
+
constructor(status: number, message: string, body?: unknown);
|
|
12
|
+
}
|
|
13
|
+
/** Thrown when a request exceeds the configured timeout. */
|
|
14
|
+
export declare class OblaxTimeoutError extends OblaxError {
|
|
15
|
+
constructor(timeoutMs: number);
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,2CAA2C;AAC3C,qBAAa,UAAW,SAAQ,KAAK;gBACvB,OAAO,EAAE,MAAM;CAK5B;AAED,+DAA+D;AAC/D,qBAAa,cAAe,SAAQ,UAAU;IAC5C,+CAA+C;IAC/C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,+CAA+C;IAC/C,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAA;gBAEV,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;CAO5D;AAED,4DAA4D;AAC5D,qBAAa,iBAAkB,SAAQ,UAAU;gBACnC,SAAS,EAAE,MAAM;CAK9B"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** Base error for all Oblax SDK errors. */
|
|
2
|
+
export class OblaxError extends Error {
|
|
3
|
+
constructor(message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = 'OblaxError';
|
|
6
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/** Thrown when the backend returns a non-2xx HTTP response. */
|
|
10
|
+
export class OblaxHttpError extends OblaxError {
|
|
11
|
+
constructor(status, message, body) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = 'OblaxHttpError';
|
|
14
|
+
this.status = status;
|
|
15
|
+
this.body = body;
|
|
16
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** Thrown when a request exceeds the configured timeout. */
|
|
20
|
+
export class OblaxTimeoutError extends OblaxError {
|
|
21
|
+
constructor(timeoutMs) {
|
|
22
|
+
super(`Request timed out after ${timeoutMs}ms`);
|
|
23
|
+
this.name = 'OblaxTimeoutError';
|
|
24
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,2CAA2C;AAC3C,MAAM,OAAO,UAAW,SAAQ,KAAK;IACnC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,YAAY,CAAA;QACxB,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;IACnD,CAAC;CACF;AAED,+DAA+D;AAC/D,MAAM,OAAO,cAAe,SAAQ,UAAU;IAM5C,YAAY,MAAc,EAAE,OAAe,EAAE,IAAc;QACzD,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAA;QAC5B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;IACnD,CAAC;CACF;AAED,4DAA4D;AAC5D,MAAM,OAAO,iBAAkB,SAAQ,UAAU;IAC/C,YAAY,SAAiB;QAC3B,KAAK,CAAC,2BAA2B,SAAS,IAAI,CAAC,CAAA;QAC/C,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAA;QAC/B,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;IACnD,CAAC;CACF"}
|
package/dist/http.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { HttpClient, OblaxPlatform, OblaxResolvedConfig, RequestOptions } from './types.js';
|
|
2
|
+
export declare class FetchHttpClient implements HttpClient {
|
|
3
|
+
private readonly config;
|
|
4
|
+
private readonly platform;
|
|
5
|
+
/**
|
|
6
|
+
* In-flight refresh promise. If a 401 arrives while a refresh is already
|
|
7
|
+
* running, we return `false` immediately to avoid concurrent refreshes and
|
|
8
|
+
* infinite loops (the refresh request itself may also 401).
|
|
9
|
+
*/
|
|
10
|
+
private refreshInFlight;
|
|
11
|
+
constructor(config: OblaxResolvedConfig, platform: OblaxPlatform);
|
|
12
|
+
/**
|
|
13
|
+
* Delegates to `platform.onTokenExpired()` while ensuring only one refresh
|
|
14
|
+
* attempt runs at a time. A second 401 while a refresh is in flight returns
|
|
15
|
+
* `false` immediately, which also breaks the cycle if the refresh request
|
|
16
|
+
* itself receives a 401.
|
|
17
|
+
*/
|
|
18
|
+
private triggerTokenRefresh;
|
|
19
|
+
get<T = unknown>(path: string, options?: RequestOptions): Promise<T>;
|
|
20
|
+
post<T = unknown>(path: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
21
|
+
put<T = unknown>(path: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
22
|
+
patch<T = unknown>(path: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
23
|
+
delete<T = unknown>(path: string, options?: RequestOptions): Promise<T>;
|
|
24
|
+
private request;
|
|
25
|
+
private shouldRetry;
|
|
26
|
+
private parseErrorResponse;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAGhG,qBAAa,eAAgB,YAAW,UAAU;IAS9C,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAT3B;;;;OAIG;IACH,OAAO,CAAC,eAAe,CAAgC;gBAGpC,MAAM,EAAE,mBAAmB,EAC3B,QAAQ,EAAE,aAAa;IAG1C;;;;;OAKG;IACH,OAAO,CAAC,mBAAmB;IAa3B,GAAG,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC;IAIpE,IAAI,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC;IAIrF,GAAG,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC;IAIpF,KAAK,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC;IAItF,MAAM,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC;YAIzD,OAAO;IA4FrB,OAAO,CAAC,WAAW;YAIL,kBAAkB;CAUjC"}
|
package/dist/http.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { OblaxHttpError, OblaxTimeoutError } from './errors.js';
|
|
2
|
+
export class FetchHttpClient {
|
|
3
|
+
constructor(config, platform) {
|
|
4
|
+
this.config = config;
|
|
5
|
+
this.platform = platform;
|
|
6
|
+
/**
|
|
7
|
+
* In-flight refresh promise. If a 401 arrives while a refresh is already
|
|
8
|
+
* running, we return `false` immediately to avoid concurrent refreshes and
|
|
9
|
+
* infinite loops (the refresh request itself may also 401).
|
|
10
|
+
*/
|
|
11
|
+
this.refreshInFlight = null;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Delegates to `platform.onTokenExpired()` while ensuring only one refresh
|
|
15
|
+
* attempt runs at a time. A second 401 while a refresh is in flight returns
|
|
16
|
+
* `false` immediately, which also breaks the cycle if the refresh request
|
|
17
|
+
* itself receives a 401.
|
|
18
|
+
*/
|
|
19
|
+
triggerTokenRefresh() {
|
|
20
|
+
if (this.refreshInFlight !== null)
|
|
21
|
+
return Promise.resolve(false);
|
|
22
|
+
this.refreshInFlight = this.platform
|
|
23
|
+
.onTokenExpired()
|
|
24
|
+
.catch(() => false)
|
|
25
|
+
.finally(() => {
|
|
26
|
+
this.refreshInFlight = null;
|
|
27
|
+
});
|
|
28
|
+
return this.refreshInFlight;
|
|
29
|
+
}
|
|
30
|
+
get(path, options) {
|
|
31
|
+
return this.request('GET', path, undefined, options);
|
|
32
|
+
}
|
|
33
|
+
post(path, body, options) {
|
|
34
|
+
return this.request('POST', path, body, options);
|
|
35
|
+
}
|
|
36
|
+
put(path, body, options) {
|
|
37
|
+
return this.request('PUT', path, body, options);
|
|
38
|
+
}
|
|
39
|
+
patch(path, body, options) {
|
|
40
|
+
return this.request('PATCH', path, body, options);
|
|
41
|
+
}
|
|
42
|
+
delete(path, options) {
|
|
43
|
+
return this.request('DELETE', path, undefined, options);
|
|
44
|
+
}
|
|
45
|
+
async request(method, path, body, options, attempt = 0, isRefreshRetry = false) {
|
|
46
|
+
const version = options?.version ?? this.config.version;
|
|
47
|
+
const url = `${this.config.endpoint}/api/${version}${path}`;
|
|
48
|
+
const hasBody = body !== undefined;
|
|
49
|
+
const isFormData = hasBody && typeof FormData !== 'undefined' && body instanceof FormData;
|
|
50
|
+
let init = {
|
|
51
|
+
method,
|
|
52
|
+
headers: {
|
|
53
|
+
Accept: 'application/json',
|
|
54
|
+
// Identifies the client application to the backend on every request.
|
|
55
|
+
'oblax-client-id': this.config.clientId,
|
|
56
|
+
// Only set Content-Type when the request actually carries a body.
|
|
57
|
+
// Sending it on GET/DELETE can cause some strict servers to reject the request.
|
|
58
|
+
// For FormData, leave Content-Type unset so fetch can set the correct
|
|
59
|
+
// multipart boundary automatically.
|
|
60
|
+
...(hasBody && !isFormData && { 'Content-Type': 'application/json' }),
|
|
61
|
+
...options?.headers,
|
|
62
|
+
},
|
|
63
|
+
...(hasBody && { body: isFormData ? body : JSON.stringify(body) }),
|
|
64
|
+
};
|
|
65
|
+
// Platform attaches auth credentials (cookie flag, bearer token, etc.)
|
|
66
|
+
init = await this.platform.prepareRequest(init);
|
|
67
|
+
// Create a timeout controller. If the caller also provided a signal, wire it
|
|
68
|
+
// in so that either the timeout or the caller can cancel the request.
|
|
69
|
+
const controller = new AbortController();
|
|
70
|
+
const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
|
|
71
|
+
options?.signal?.addEventListener('abort', () => {
|
|
72
|
+
controller.abort(options.signal?.reason);
|
|
73
|
+
});
|
|
74
|
+
init.signal = controller.signal;
|
|
75
|
+
try {
|
|
76
|
+
const response = await fetch(url, init);
|
|
77
|
+
clearTimeout(timeoutId);
|
|
78
|
+
if (!response.ok) {
|
|
79
|
+
// Attempt a silent token refresh on the first 401, then retry once.
|
|
80
|
+
if (response.status === 401 && !isRefreshRetry) {
|
|
81
|
+
const refreshed = await this.triggerTokenRefresh();
|
|
82
|
+
if (refreshed) {
|
|
83
|
+
return this.request(method, path, body, options, attempt, true);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const error = await this.parseErrorResponse(response);
|
|
87
|
+
if (this.shouldRetry(error.status, attempt)) {
|
|
88
|
+
return this.request(method, path, body, options, attempt + 1, isRefreshRetry);
|
|
89
|
+
}
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
if (response.status === 204) {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
// A 2xx with a non-JSON body (e.g. text/plain "OK" from cookie-based
|
|
96
|
+
// auth responses) has nothing to decode as JSON; resolve `undefined`
|
|
97
|
+
// instead of throwing on `response.json()`.
|
|
98
|
+
const contentType = response.headers.get('content-type') ?? '';
|
|
99
|
+
if (!contentType.includes('application/json')) {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
return response.json();
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
clearTimeout(timeoutId);
|
|
106
|
+
if (err instanceof Error && err.name === 'AbortError') {
|
|
107
|
+
// If the caller's signal aborted, re-throw as-is so the caller can
|
|
108
|
+
// distinguish their own cancellation from a timeout.
|
|
109
|
+
if (options?.signal?.aborted) {
|
|
110
|
+
throw err;
|
|
111
|
+
}
|
|
112
|
+
throw new OblaxTimeoutError(this.config.timeout);
|
|
113
|
+
}
|
|
114
|
+
throw err;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
shouldRetry(status, attempt) {
|
|
118
|
+
return attempt < this.config.retries && status >= 500;
|
|
119
|
+
}
|
|
120
|
+
async parseErrorResponse(response) {
|
|
121
|
+
try {
|
|
122
|
+
const body = (await response.json());
|
|
123
|
+
const message = typeof body['message'] === 'string' ? body['message'] : response.statusText;
|
|
124
|
+
return new OblaxHttpError(response.status, message, body);
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
return new OblaxHttpError(response.status, response.statusText);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=http.js.map
|
package/dist/http.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAE/D,MAAM,OAAO,eAAe;IAQ1B,YACmB,MAA2B,EAC3B,QAAuB;QADvB,WAAM,GAAN,MAAM,CAAqB;QAC3B,aAAQ,GAAR,QAAQ,CAAe;QAT1C;;;;WAIG;QACK,oBAAe,GAA4B,IAAI,CAAA;IAKpD,CAAC;IAEJ;;;;;OAKG;IACK,mBAAmB;QACzB,IAAI,IAAI,CAAC,eAAe,KAAK,IAAI;YAAE,OAAO,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QAEhE,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,QAAQ;aACjC,cAAc,EAAE;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I,GAAG,YAAY,KAAK,IAAI,GAAG,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;gBACtD,mEAAmE;gBACnE,qDAAqD;gBACrD,IAAI,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;oBAC7B,MAAM,GAAG,CAAA;gBACX,CAAC;gBACD,MAAM,IAAI,iBAAiB,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;YAClD,CAAC;YAED,MAAM,GAAG,CAAA;QACX,CAAC;IACH,CAAC;IAEO,WAAW,CAAC,MAAc,EAAE,OAAe;QACjD,OAAO,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,IAAI,MAAM,IAAI,GAAG,CAAA;IACvD,CAAC;IAEO,KAAK,CAAC,kBAAkB,CAAC,QAAkB;QACjD,IAAI,CAAC;YACH,MAAM,IAAI,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAA4B,CAAA;YAC/D,MAAM,OAAO,GACX,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAA;YAC7E,OAAO,IAAI,cAAc,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,CAAA;QAC3D,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,IAAI,cAAc,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,UAAU,CAAC,CAAA;QACjE,CAAC;IACH,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { Oblax } from './oblax.js';
|
|
2
|
+
export type { OblaxConstructor } from './oblax.js';
|
|
3
|
+
export { OblaxError, OblaxHttpError, OblaxTimeoutError } from './errors.js';
|
|
4
|
+
export type { OblaxConfig, OblaxEnvironment, OblaxResolvedConfig, OblaxPlatform, CredentialDelivery, AuthPayload, OblaxModulePlugin, OblaxContext, HttpClient, RequestOptions, AnyModulePlugin, ModulesShape, } from './types.js';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,YAAY,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAElD,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAE3E,YAAY,EACV,WAAW,EACX,gBAAgB,EAChB,mBAAmB,EACnB,aAAa,EACb,kBAAkB,EAClB,WAAW,EACX,iBAAiB,EACjB,YAAY,EACZ,UAAU,EACV,cAAc,EACd,eAAe,EACf,YAAY,GACb,MAAM,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAGlC,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA"}
|
package/dist/oblax.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { AnyModulePlugin, ModulesShape, OblaxConfig } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Type-safe constructor signature for the Oblax SDK.
|
|
4
|
+
*
|
|
5
|
+
* TypeScript infers each module's type from the `modules` tuple, so
|
|
6
|
+
* `oblax.auth`, `oblax.platform`, etc. are fully typed without annotations.
|
|
7
|
+
*/
|
|
8
|
+
export type OblaxConstructor = {
|
|
9
|
+
new <T extends readonly AnyModulePlugin[]>(config: OblaxConfig<T>): ModulesShape<T>;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* The Oblax SDK entry point.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const oblax = new Oblax({
|
|
17
|
+
* endpoint: 'https://api.my-app.oblax.io',
|
|
18
|
+
* clientId: 'my-web-app',
|
|
19
|
+
* modules: [PlatformBrowser(), Auth()],
|
|
20
|
+
* })
|
|
21
|
+
*
|
|
22
|
+
* await oblax.auth.session.create({ username: 'alice', password: 'secret' })
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare const Oblax: OblaxConstructor;
|
|
26
|
+
//# sourceMappingURL=oblax.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oblax.d.ts","sourceRoot":"","sources":["../src/oblax.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,YAAY,EACZ,WAAW,EAIZ,MAAM,YAAY,CAAA;AA0FnB;;;;;GAKG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,KAAK,CAAC,SAAS,SAAS,eAAe,EAAE,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAAA;CACpF,CAAA;AAED;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,KAAK,EAA2B,gBAAgB,CAAA"}
|
package/dist/oblax.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { FetchHttpClient } from './http.js';
|
|
2
|
+
import { OblaxError } from './errors.js';
|
|
3
|
+
const PLATFORM_MODULE_NAME = 'platform';
|
|
4
|
+
const DEFAULTS = {
|
|
5
|
+
environment: 'production',
|
|
6
|
+
retries: 1,
|
|
7
|
+
timeout: 10000,
|
|
8
|
+
version: 'v1',
|
|
9
|
+
};
|
|
10
|
+
class OblaxImpl {
|
|
11
|
+
constructor(config) {
|
|
12
|
+
// 1. Resolve config with defaults
|
|
13
|
+
const resolved = {
|
|
14
|
+
endpoint: config.endpoint.replace(/\/$/, ''),
|
|
15
|
+
clientId: config.clientId,
|
|
16
|
+
environment: config.environment ?? DEFAULTS.environment,
|
|
17
|
+
retries: config.retries ?? DEFAULTS.retries,
|
|
18
|
+
timeout: config.timeout ?? DEFAULTS.timeout,
|
|
19
|
+
version: config.version ?? DEFAULTS.version,
|
|
20
|
+
};
|
|
21
|
+
// Fail-fast: `clientId` is required on every request, so a missing or blank
|
|
22
|
+
// value must abort construction — never the first outbound request.
|
|
23
|
+
if (!resolved.clientId || resolved.clientId.trim() === '') {
|
|
24
|
+
throw new OblaxError('Oblax config: `clientId` is required and must not be empty. ' +
|
|
25
|
+
'Pass the client identifier issued for your application, e.g. ' +
|
|
26
|
+
"new Oblax({ endpoint: '...', clientId: 'my-app', modules: [...] })");
|
|
27
|
+
}
|
|
28
|
+
const modules = config.modules;
|
|
29
|
+
// 2. Validate: no duplicate module names
|
|
30
|
+
const seen = new Set();
|
|
31
|
+
for (const plugin of modules) {
|
|
32
|
+
if (seen.has(plugin.name)) {
|
|
33
|
+
throw new OblaxError(`Duplicate module name "${plugin.name}". Each module must appear only once.`);
|
|
34
|
+
}
|
|
35
|
+
seen.add(plugin.name);
|
|
36
|
+
}
|
|
37
|
+
// 3. Locate and validate the platform module
|
|
38
|
+
const platformPlugin = modules.find((m) => m.name === PLATFORM_MODULE_NAME);
|
|
39
|
+
if (!platformPlugin) {
|
|
40
|
+
throw new OblaxError('No platform module was found. ' +
|
|
41
|
+
'Add a platform module (e.g. PlatformBrowser() or PlatformServer()) ' +
|
|
42
|
+
'to the `modules` array in your Oblax configuration.');
|
|
43
|
+
}
|
|
44
|
+
// 4. Initialise the platform.
|
|
45
|
+
// Platform create() receives the config only — it must not call ctx.http or
|
|
46
|
+
// ctx.platform during initialisation, as neither exists yet at this point.
|
|
47
|
+
const platformContext = {
|
|
48
|
+
config: resolved,
|
|
49
|
+
http: undefined,
|
|
50
|
+
platform: undefined,
|
|
51
|
+
};
|
|
52
|
+
const platform = platformPlugin.create(platformContext);
|
|
53
|
+
// 5. Build the HTTP client with the resolved platform
|
|
54
|
+
const http = new FetchHttpClient(resolved, platform);
|
|
55
|
+
// 6. Build the full shared context for domain modules
|
|
56
|
+
const context = { config: resolved, http, platform };
|
|
57
|
+
// 7. Initialise all modules and attach them to this instance.
|
|
58
|
+
// The platform is reused directly — its create() must not be called again,
|
|
59
|
+
// otherwise oblax.platform would be a different object than the one wired
|
|
60
|
+
// into the HTTP client, breaking token state on ServerPlatform.
|
|
61
|
+
for (const plugin of modules) {
|
|
62
|
+
const instance = plugin.name === PLATFORM_MODULE_NAME
|
|
63
|
+
? platform
|
|
64
|
+
: plugin.create(context);
|
|
65
|
+
// @ts-expect-error — intentional dynamic assignment; the public
|
|
66
|
+
// OblaxConstructor type below provides full type safety for consumers.
|
|
67
|
+
this[plugin.name] = instance;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The Oblax SDK entry point.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const oblax = new Oblax({
|
|
77
|
+
* endpoint: 'https://api.my-app.oblax.io',
|
|
78
|
+
* clientId: 'my-web-app',
|
|
79
|
+
* modules: [PlatformBrowser(), Auth()],
|
|
80
|
+
* })
|
|
81
|
+
*
|
|
82
|
+
* await oblax.auth.session.create({ username: 'alice', password: 'secret' })
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export const Oblax = OblaxImpl;
|
|
86
|
+
//# sourceMappingURL=oblax.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oblax.js","sourceRoot":"","sources":["../src/oblax.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAExC,MAAM,oBAAoB,GAAG,UAAmB,CAAA;AAEhD,MAAM,QAAQ,GAAG;IACf,WAAW,EAAE,YAAqB;IAClC,OAAO,EAAE,CAAC;I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AC,CAAC,CAAC,IAAI,KAAK,oBAAoB,CAAC,CAAA;QAE3E,IAAI,CAAC,cAAc,EAAE,CAAC;YACpB,MAAM,IAAI,UAAU,CAClB,gCAAgC;gBAChC,qEAAqE;gBACrE,qDAAqD,CACtD,CAAA;QACH,CAAC;QAED,8BAA8B;QAC9B,4EAA4E;QAC5E,2EAA2E;QAC3E,MAAM,eAAe,GAAiB;YACpC,MAAM,EAAE,QAAQ;YAChB,IAAI,EAAE,SAAkB;YACxB,QAAQ,EAAE,SAAkB;SAC7B,CAAA;QACD,MAAM,QAAQ,GAAG,cAAc,CAAC,MAAM,CAAC,eAAe,CAAkB,CAAA;QAExE,sDAAsD;QACtD,MAAM,IAAI,GAAG,IAAI,eAAe,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAA;QAEpD,sDAAsD;QACtD,MAAM,OAAO,GAAiB,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAA;QAElE,8DAA8D;QAC9D,8EAA8E;QAC9E,6EAA6E;QAC7E,mEAAmE;QACnE,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC7B,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,KAAK,oBAAoB;gBACnD,CAAC,CAAC,QAAQ;gBACV,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;YAC1B,gEAAgE;YAChE,uEAAuE;YACvE,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAA;QAC9B,CAAC;IACH,CAAC;CACF;AAYD;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,SAAwC,CAAA"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/** Runtime environment the SDK is operating in. */
|
|
2
|
+
export type OblaxEnvironment = 'development' | 'staging' | 'production';
|
|
3
|
+
/**
|
|
4
|
+
* Credentials returned by the backend on a successful authentication call.
|
|
5
|
+
* Platforms use this to persist auth state (store token, read cookie, etc.).
|
|
6
|
+
*/
|
|
7
|
+
export interface AuthPayload {
|
|
8
|
+
/** Short-lived JWT access token. Present on token-based platforms. */
|
|
9
|
+
oblax_access_token?: string;
|
|
10
|
+
/** Long-lived refresh token. Present on token-based platforms. */
|
|
11
|
+
oblax_refresh_token?: string;
|
|
12
|
+
/** Unix timestamp (seconds) when the access token expires. */
|
|
13
|
+
expiration?: number;
|
|
14
|
+
/** Arbitrary extra fields returned by the backend. */
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* How a platform receives credentials from a successful auth response.
|
|
19
|
+
*
|
|
20
|
+
* - `'cookie'` — the server sets HTTP-only cookies via `Set-Cookie`; the
|
|
21
|
+
* response body carries no JS-readable tokens (browser platforms).
|
|
22
|
+
* - `'token'` — credentials are returned in the JSON body and the platform
|
|
23
|
+
* stores them in memory to attach as a `Bearer` token (server/CLI/mobile).
|
|
24
|
+
*/
|
|
25
|
+
export type CredentialDelivery = 'cookie' | 'token';
|
|
26
|
+
/**
|
|
27
|
+
* Platform contract.
|
|
28
|
+
*
|
|
29
|
+
* Controls how credentials are attached to requests, persisted after login,
|
|
30
|
+
* and cleared on logout. Each runtime (browser, server, React Native…) ships
|
|
31
|
+
* its own package that implements this interface.
|
|
32
|
+
*/
|
|
33
|
+
export interface OblaxPlatform {
|
|
34
|
+
/**
|
|
35
|
+
* Enriches a `RequestInit` before it is passed to `fetch`.
|
|
36
|
+
* Use this to add `Authorization` headers, set `credentials: 'include'`, etc.
|
|
37
|
+
*/
|
|
38
|
+
prepareRequest(init: RequestInit): Promise<RequestInit>;
|
|
39
|
+
/**
|
|
40
|
+
* How this platform receives auth credentials from the server. Auth-aware
|
|
41
|
+
* modules (e.g. `session.create`) use this to decide whether the response
|
|
42
|
+
* body carries tokens (`'token'`) or whether credentials arrive purely via
|
|
43
|
+
* HTTP-only cookies (`'cookie'`), so the module can skip parsing a body it
|
|
44
|
+
* knows it will not receive.
|
|
45
|
+
*/
|
|
46
|
+
readonly credentialDelivery: CredentialDelivery;
|
|
47
|
+
/**
|
|
48
|
+
* Called after a successful login response.
|
|
49
|
+
* Use this to store the received token or react to the auth cookie.
|
|
50
|
+
*/
|
|
51
|
+
onAuthSuccess(payload: AuthPayload): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Called on logout or when credentials become invalid.
|
|
54
|
+
* Use this to clear any stored state.
|
|
55
|
+
*/
|
|
56
|
+
onAuthClear(): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Returns the stored refresh token, or `null` if the platform does not
|
|
59
|
+
* keep one (e.g. cookie-based browser platforms).
|
|
60
|
+
*
|
|
61
|
+
* Used by `session.refresh()` to satisfy backends that require the
|
|
62
|
+
* refresh token in the request body for token-based clients.
|
|
63
|
+
*/
|
|
64
|
+
getRefreshToken(): Promise<string | null>;
|
|
65
|
+
/**
|
|
66
|
+
* Called by the HTTP client when a 401 response is received.
|
|
67
|
+
* Should attempt to refresh the session and return `true` on success.
|
|
68
|
+
* Return `false` (or throw) if the session cannot be renewed.
|
|
69
|
+
*
|
|
70
|
+
* The auth module overwrites this property after initialization to wire in
|
|
71
|
+
* `session.refresh()`. The default implementation returns `false` (no-op).
|
|
72
|
+
*/
|
|
73
|
+
onTokenExpired: () => Promise<boolean>;
|
|
74
|
+
}
|
|
75
|
+
/** Per-request options modules may pass to the HTTP client. */
|
|
76
|
+
export interface RequestOptions {
|
|
77
|
+
/** Additional headers merged on top of the defaults. */
|
|
78
|
+
headers?: Record<string, string>;
|
|
79
|
+
/** AbortSignal for request cancellation. */
|
|
80
|
+
signal?: AbortSignal;
|
|
81
|
+
/**
|
|
82
|
+
* Overrides the SDK-level default API version for this specific request.
|
|
83
|
+
* Use this when a single endpoint has been promoted to a newer version
|
|
84
|
+
* while the rest of the API remains on the default version.
|
|
85
|
+
* @example 'v2'
|
|
86
|
+
*/
|
|
87
|
+
version?: string;
|
|
88
|
+
}
|
|
89
|
+
/** Thin HTTP client exposed to modules via the SDK context. */
|
|
90
|
+
export interface HttpClient {
|
|
91
|
+
get<T = unknown>(path: string, options?: RequestOptions): Promise<T>;
|
|
92
|
+
post<T = unknown>(path: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
93
|
+
put<T = unknown>(path: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
94
|
+
patch<T = unknown>(path: string, body?: unknown, options?: RequestOptions): Promise<T>;
|
|
95
|
+
delete<T = unknown>(path: string, options?: RequestOptions): Promise<T>;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The subset of SDK configuration readable by modules at runtime.
|
|
99
|
+
* All fields are read-only — config is immutable after `new Oblax(...)`.
|
|
100
|
+
*/
|
|
101
|
+
export interface OblaxResolvedConfig {
|
|
102
|
+
readonly endpoint: string;
|
|
103
|
+
readonly clientId: string;
|
|
104
|
+
readonly environment: OblaxEnvironment;
|
|
105
|
+
readonly retries: number;
|
|
106
|
+
readonly timeout: number;
|
|
107
|
+
readonly version: string;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Shared context injected into every module during initialization.
|
|
111
|
+
*
|
|
112
|
+
* Modules MUST obtain all shared services from here.
|
|
113
|
+
* Modules MUST NOT receive platform services through their own configuration.
|
|
114
|
+
*/
|
|
115
|
+
export interface OblaxContext {
|
|
116
|
+
/** Resolved, immutable SDK configuration. */
|
|
117
|
+
readonly config: OblaxResolvedConfig;
|
|
118
|
+
/** HTTP client pre-configured for this SDK instance. */
|
|
119
|
+
readonly http: HttpClient;
|
|
120
|
+
/** Active platform implementation. */
|
|
121
|
+
readonly platform: OblaxPlatform;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* A module plugin — the return type of every module factory function.
|
|
125
|
+
*
|
|
126
|
+
* `TName` — string literal that becomes the property name on the Oblax instance.
|
|
127
|
+
* `TInstance` — the public API object the module exposes.
|
|
128
|
+
*/
|
|
129
|
+
export interface OblaxModulePlugin<TName extends string, TInstance> {
|
|
130
|
+
/** Unique module name. Must be a stable string literal. */
|
|
131
|
+
readonly name: TName;
|
|
132
|
+
/**
|
|
133
|
+
* Called once during `new Oblax(...)`.
|
|
134
|
+
* Must throw on invalid configuration (fail-fast at startup).
|
|
135
|
+
*/
|
|
136
|
+
create(context: OblaxContext): TInstance;
|
|
137
|
+
}
|
|
138
|
+
export type AnyModulePlugin = OblaxModulePlugin<string, any>;
|
|
139
|
+
/**
|
|
140
|
+
* Maps a tuple of module plugins to a typed object shape.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* type Shape = ModulesShape<[OblaxModulePlugin<'auth', AuthModule>, OblaxModulePlugin<'platform', BrowserPlatform>]>
|
|
144
|
+
* // => { auth: AuthModule; platform: BrowserPlatform }
|
|
145
|
+
*/
|
|
146
|
+
export type ModulesShape<T extends readonly AnyModulePlugin[]> = {
|
|
147
|
+
[K in T[number] as K['name']]: ReturnType<K['create']>;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* Configuration object passed to `new Oblax(config)`.
|
|
151
|
+
*
|
|
152
|
+
* The generic `T` is inferred from the `modules` array and drives the type
|
|
153
|
+
* of the resulting Oblax instance — no manual type annotations required.
|
|
154
|
+
*/
|
|
155
|
+
export interface OblaxConfig<T extends readonly AnyModulePlugin[] = readonly AnyModulePlugin[]> {
|
|
156
|
+
/**
|
|
157
|
+
* Base URL of the Oblax backend API. Trailing slashes are stripped.
|
|
158
|
+
* @example 'https://api.my-app.oblax.io'
|
|
159
|
+
*/
|
|
160
|
+
endpoint: string;
|
|
161
|
+
/**
|
|
162
|
+
* Identifies the client application making requests to the Oblax backend.
|
|
163
|
+
* Sent on every request as the `oblax-client-id` header so the backend can
|
|
164
|
+
* attribute traffic, enforce per-client policy, and rate-limit appropriately.
|
|
165
|
+
*
|
|
166
|
+
* Required — omitting it throws at construction time (fail-fast).
|
|
167
|
+
* @example 'my-web-app'
|
|
168
|
+
*/
|
|
169
|
+
clientId: string;
|
|
170
|
+
/**
|
|
171
|
+
* Declares the runtime environment. Modules may use this to adjust behaviour
|
|
172
|
+
* (e.g. verbose logging in development).
|
|
173
|
+
* @default 'production'
|
|
174
|
+
*/
|
|
175
|
+
environment?: OblaxEnvironment;
|
|
176
|
+
/**
|
|
177
|
+
* Maximum number of additional attempts after the first failed request.
|
|
178
|
+
* Only 5xx responses are retried. Set to `0` to disable.
|
|
179
|
+
* @default 1
|
|
180
|
+
*/
|
|
181
|
+
retries?: number;
|
|
182
|
+
/**
|
|
183
|
+
* Per-request timeout in milliseconds.
|
|
184
|
+
* @default 10000
|
|
185
|
+
*/
|
|
186
|
+
timeout?: number;
|
|
187
|
+
/**
|
|
188
|
+
* Default API version to use for all requests. Applied as the `/api/{version}`
|
|
189
|
+
* prefix on every HTTP call. Individual methods can override this per-request
|
|
190
|
+
* via `RequestOptions.version`.
|
|
191
|
+
* @default 'v1'
|
|
192
|
+
*/
|
|
193
|
+
version?: string;
|
|
194
|
+
/**
|
|
195
|
+
* Modules to activate. Must include exactly one platform module (`name: 'platform'`).
|
|
196
|
+
* Configuration errors are thrown at construction time, not at request time.
|
|
197
|
+
*/
|
|
198
|
+
modules: T;
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA,mDAAmD;AACnD,MAAM,MAAM,gBAAgB,GAAG,aAAa,GAAG,SAAS,GAAG,YAAY,CAAA;AAEvE;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,sEAAsE;IACtE,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,kEAAkE;IAClE,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAC5B,8DAA8D;IAC9D,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,sDAAsD;IACtD,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA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Ae,EAAE,IAAI;KAC9D,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;CACvD,CAAA;AAID;;;;;GAKG;AACH,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,SAAS,eAAe,EAAE,GAAG,SAAS,eAAe,EAAE;IAC5F;;;OAGG;IACH,QAAQ,EAAE,MAAM,CAAA;IAEhB;;;;;;;OAOG;IACH,QAAQ,EAAE,MAAM,CAAA;IAEhB;;;;OAIG;IACH,WAAW,CAAC,EAAE,gBAAgB,CAAA;IAE9B;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAEhB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAEhB;;;;;OAKG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAEhB;;;OAGG;IACH,OAAO,EAAE,CAAC,CAAA;CACX"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,gFAAgF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oblax/core",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Oblax SDK — core package",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Oblax SDK for JavaScript Team",
|
|
8
|
+
"url": "https://docs.oblax.io/docs/getting-started/your-first-sdk-call"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/oblaxio/oblax-sdk-js#readme",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/oblaxio/oblax-sdk-js.git",
|
|
14
|
+
"directory": "packages/core"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/oblaxio/oblax-sdk-js/issues"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"oblax",
|
|
21
|
+
"sdk",
|
|
22
|
+
"core"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"import": "./dist/index.js",
|
|
30
|
+
"types": "./dist/index.d.ts"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"typescript": "^5.5.0"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsc --build tsconfig.json",
|
|
44
|
+
"typecheck": "tsc --build tsconfig.json --dry",
|
|
45
|
+
"clean": "rm -rf dist tsconfig.tsbuildinfo"
|
|
46
|
+
}
|
|
47
|
+
}
|