@realnation/builder-shared-sdk 0.1.0 → 0.1.1

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/http.d.ts CHANGED
@@ -2,6 +2,13 @@ import { type AxiosInstance } from 'axios';
2
2
  import type { RequestContext } from './types';
3
3
  export interface HttpClientOptions extends RequestContext {
4
4
  }
5
+ type TokenResolver = () => string | null;
6
+ export declare function setAuthToken(token: string | null): void;
7
+ export declare function getAuthToken(): string | null;
8
+ export declare function setTokenResolver(resolver: TokenResolver | null): void;
5
9
  export declare function createHttpClient(options: HttpClientOptions): AxiosInstance;
6
- export declare function attachAuthHeader(instance: AxiosInstance, tokenResolver: () => string | null): void;
10
+ export declare function configureHttpClient(options: HttpClientOptions): AxiosInstance;
11
+ export declare function getHttpClient(): AxiosInstance;
12
+ export declare function attachAuthHeader(instance: AxiosInstance, tokenResolver?: TokenResolver): void;
13
+ export {};
7
14
  //# sourceMappingURL=http.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,KAAK,aAAa,EAA6D,MAAM,OAAO,CAAC;AAC7G,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE9C,MAAM,WAAW,iBAAkB,SAAQ,cAAc;CAAG;AAE5D,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,iBAAiB,GAAG,aAAa,CAQ1E;AAED,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,MAAM,GAAG,IAAI,GAAG,IAAI,CASlG"}
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,KAAK,aAAa,EAA6D,MAAM,OAAO,CAAC;AAC7G,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE9C,MAAM,WAAW,iBAAkB,SAAQ,cAAc;CAAG;AAE5D,KAAK,aAAa,GAAG,MAAM,MAAM,GAAG,IAAI,CAAC;AAazC,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAEvD;AAED,wBAAgB,YAAY,IAAI,MAAM,GAAG,IAAI,CAE5C;AAED,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,aAAa,GAAG,IAAI,GAAG,IAAI,CAErE;AAED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,iBAAiB,GAAG,aAAa,CAS1E;AAED,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,iBAAiB,GAAG,aAAa,CAG7E;AAED,wBAAgB,aAAa,IAAI,aAAa,CAK7C;AAED,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,aAAa,EAAE,aAAa,CAAC,EAAE,aAAa,GAAG,IAAI,CAS7F"}
package/dist/http.js CHANGED
@@ -1,15 +1,44 @@
1
1
  import axios from 'axios';
2
+ let currentToken = null;
3
+ let resolverOverride = null;
4
+ let sharedClient = null;
5
+ function resolveToken() {
6
+ if (resolverOverride) {
7
+ return resolverOverride();
8
+ }
9
+ return currentToken;
10
+ }
11
+ export function setAuthToken(token) {
12
+ currentToken = token ?? null;
13
+ }
14
+ export function getAuthToken() {
15
+ return currentToken;
16
+ }
17
+ export function setTokenResolver(resolver) {
18
+ resolverOverride = resolver;
19
+ }
2
20
  export function createHttpClient(options) {
3
21
  const instance = axios.create({
4
22
  baseURL: options.baseURL,
5
23
  timeout: options.timeout ?? 15000,
6
24
  headers: options.headers
7
25
  });
26
+ attachAuthHeader(instance);
8
27
  return instance;
9
28
  }
29
+ export function configureHttpClient(options) {
30
+ sharedClient = createHttpClient(options);
31
+ return sharedClient;
32
+ }
33
+ export function getHttpClient() {
34
+ if (!sharedClient) {
35
+ throw new Error('[builder-shared-sdk] HTTP client 尚未初始化,请先调用 configureHttpClient。');
36
+ }
37
+ return sharedClient;
38
+ }
10
39
  export function attachAuthHeader(instance, tokenResolver) {
11
40
  instance.interceptors.request.use((config) => {
12
- const token = tokenResolver();
41
+ const token = (tokenResolver ?? resolveToken)();
13
42
  if (token) {
14
43
  config.headers = config.headers ?? {};
15
44
  config.headers.Authorization = `Bearer ${token}`;
package/dist/types.d.ts CHANGED
@@ -8,7 +8,17 @@ export interface RequestContext {
8
8
  headers?: Record<string, string>;
9
9
  }
10
10
  export type EventPayload = Record<string, unknown>;
11
- export type SdkEventName = 'sdk:ready' | 'sdk:error' | 'module:navigate' | 'module:metrics' | 'module:error' | 'host:refresh-token';
11
+ export type SdkEventName = 'sdk:ready' | 'sdk:error' | 'module:navigate' | 'module:metrics' | 'module:error' | 'module:open-surface' | 'module:close-surface' | 'host:refresh-token';
12
+ export interface ModuleSurfaceRequest extends EventPayload {
13
+ surfaceId: string;
14
+ title?: string;
15
+ mode?: 'modal' | 'drawer';
16
+ width?: string;
17
+ props?: Record<string, unknown>;
18
+ }
19
+ export interface ModuleSurfaceCloseRequest extends EventPayload {
20
+ surfaceId?: string;
21
+ }
12
22
  export interface SdkEvent<TPayload extends EventPayload = EventPayload> {
13
23
  name: SdkEventName;
14
24
  payload: TPayload;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC;AAED,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEnD,MAAM,MAAM,YAAY,GACpB,WAAW,GACX,WAAW,GACX,iBAAiB,GACjB,gBAAgB,GAChB,cAAc,GACd,oBAAoB,CAAC;AAEzB,MAAM,WAAW,QAAQ,CAAC,QAAQ,SAAS,YAAY,GAAG,YAAY;IACpE,IAAI,EAAE,YAAY,CAAC;IACnB,OAAO,EAAE,QAAQ,CAAC;CACnB"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC;AAED,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEnD,MAAM,MAAM,YAAY,GACpB,WAAW,GACX,WAAW,GACX,iBAAiB,GACjB,gBAAgB,GAChB,cAAc,GACd,qBAAqB,GACrB,sBAAsB,GACtB,oBAAoB,CAAC;AAEzB,MAAM,WAAW,oBAAqB,SAAQ,YAAY;IACxD,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAC;IAC1B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACjC;AAED,MAAM,WAAW,yBAA0B,SAAQ,YAAY;IAC7D,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,QAAQ,CAAC,QAAQ,SAAS,YAAY,GAAG,YAAY;IACpE,IAAI,EAAE,YAAY,CAAC;IACnB,OAAO,EAAE,QAAQ,CAAC;CACnB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@realnation/builder-shared-sdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "exports": {