@onagentic/master-data 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/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # @onagentic/master-data
2
+
3
+ Lightweight TypeScript client for fetching master data from a Strapi-compatible REST API.
4
+
5
+ Supports token auth, pagination, field selection, group filtering, and runtime config updates.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @onagentic/master-data
11
+ ```
12
+
13
+ ## Quick start
14
+
15
+ ```ts
16
+ import { createClient } from '@onagentic/master-data';
17
+
18
+ const client = createClient({
19
+ baseUrl: 'https://api.example.com',
20
+ token: process.env.API_TOKEN!,
21
+ });
22
+
23
+ // Single page (default: page 1, pageSize 100, active only)
24
+ const items = await client.getItems('MASTER_ECO_SERVICES');
25
+
26
+ // All pages automatically
27
+ const all = await client.fetchAll('MASTER_ECO_SERVICES');
28
+
29
+ // Full response with pagination meta
30
+ const response = await client.fetch('MASTER_ECO_SERVICES', {
31
+ pagination: { page: 2, pageSize: 50 },
32
+ sort: { field: 'label', order: 'asc' },
33
+ fields: ['documentId', 'key', 'label'],
34
+ activeOnly: false,
35
+ });
36
+ console.log(response.meta.pagination); // { page, pageSize, pageCount, total }
37
+ ```
38
+
39
+ ## Config options
40
+
41
+ | Option | Type | Default | Description |
42
+ |---|---|---|---|
43
+ | `baseUrl` | `string` | **required** | API base URL |
44
+ | `token` | `string` | **required** | Bearer token |
45
+ | `apiPath` | `string` | `/api/master-datas` | API endpoint path |
46
+ | `defaultFields` | `string[]` | `['documentId','key','label','order','is_active']` | Fields to request |
47
+ | `defaultPageSize` | `number` | `100` | Items per page |
48
+ | `defaultSortField` | `string` | `'order'` | Field to sort by |
49
+ | `defaultSortOrder` | `'asc'\|'desc'` | `'asc'` | Sort direction |
50
+ | `timeout` | `number` | `10000` | Request timeout (ms) |
51
+
52
+ ## Fetch options
53
+
54
+ | Option | Type | Default | Description |
55
+ |---|---|---|---|
56
+ | `activeOnly` | `boolean` | `true` | Filter `is_active = true` |
57
+ | `fetchAll` | `boolean` | `false` | Auto-paginate all pages (use with `getItems`) |
58
+ | `pagination` | `{ page, pageSize }` | — | Override pagination |
59
+ | `sort` | `{ field, order }` | — | Override sort |
60
+ | `fields` | `string[]` | — | Override fields |
61
+
62
+ ## Runtime config update
63
+
64
+ ```ts
65
+ // e.g. after refreshing a token
66
+ client.updateConfig({ token: 'new-token' });
67
+ ```
68
+
69
+ ## Release
70
+
71
+ ```bash
72
+ # bump version
73
+ npm version patch # or minor / major
74
+
75
+ # build + test + publish
76
+ npm publish --access public
77
+ ```
78
+
79
+ ## License
80
+
81
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ 'use strict';var m=["documentId","key","label","order","is_active"];var C="order";function D(a){return a.map(([t,e])=>`${encodeURIComponent(t).replace(/%5B/gi,"[").replace(/%5D/gi,"]").replace(/%24/gi,"$")}=${encodeURIComponent(e)}`).join("&")}function h(a,t,e){var d,f,p,g;let r=[],n=((d=t.sort)==null?void 0:d.field)??e.defaultSortField,o=((f=t.sort)==null?void 0:f.order)??e.defaultSortOrder;r.push([`sort[${n}]`,o]);let l=((p=t.pagination)==null?void 0:p.page)??1,i=((g=t.pagination)==null?void 0:g.pageSize)??e.defaultPageSize;r.push(["pagination[page]",String(l)]),r.push(["pagination[pageSize]",String(i)]);let s=t.fields??e.defaultFields;return r.push(["fields",s.join(",")]),r.push(["filters[$and][0][groups][code][$containsi]",a]),t.activeOnly!==false&&r.push(["filters[$and][1][is_active][$eq]","true"]),D(r)}function c(a){return {baseUrl:a.baseUrl.replace(/\/$/,""),token:a.token,apiPath:a.apiPath??"/api/master-datas",defaultFields:a.defaultFields??m,defaultPageSize:a.defaultPageSize??100,defaultSortOrder:a.defaultSortOrder??"asc",defaultSortField:a.defaultSortField??C,timeout:a.timeout??1e4}}var u=class{config;constructor(t){if(!t.baseUrl)throw new Error("[master-data] baseUrl is required");if(!t.token)throw new Error("[master-data] token is required");this.config=c(t);}async fetch(t,e={}){if(!t)throw new Error("[master-data] groupCode is required");let r=h(t,e,this.config),n=`${this.config.baseUrl}${this.config.apiPath}?${r}`,o=new AbortController,l=setTimeout(()=>o.abort(),this.config.timeout),i;try{i=await fetch(n,{method:"GET",headers:{Authorization:`Bearer ${this.config.token}`,"Content-Type":"application/json"},signal:o.signal});}catch(s){throw s.name==="AbortError"?new Error(`[master-data] Request timed out after ${this.config.timeout}ms`):s}finally{clearTimeout(l);}if(!i.ok){let s=await i.text().catch(()=>"");throw new Error(`[master-data] API error ${i.status} ${i.statusText}: ${s}`)}return i.json()}async fetchAll(t,e={}){var l;let r=((l=e.pagination)==null?void 0:l.pageSize)??this.config.defaultPageSize,n=1,o=[];for(;;){let i=await this.fetch(t,{...e,pagination:{page:n,pageSize:r}});o.push(...i.data);let{pageCount:s}=i.meta.pagination;if(n>=s)break;n++;}return o}async getItems(t,e={}){return e.fetchAll?this.fetchAll(t,e):(await this.fetch(t,e)).data}updateConfig(t){let e=c({...this.config,...t});Object.assign(this.config,e);}};function S(a){return new u(a)}exports.MasterDataClient=u;exports.createClient=S;
@@ -0,0 +1,82 @@
1
+ interface MasterDataClientConfig {
2
+ /** Base URL of the API endpoint (e.g. https://api.example.com) */
3
+ baseUrl: string;
4
+ /** Bearer token for authentication */
5
+ token: string;
6
+ /** API path prefix (default: /api/master-datas) */
7
+ apiPath?: string;
8
+ /** Default fields to fetch (default: documentId,key,label,order,is_active) */
9
+ defaultFields?: string[];
10
+ /** Default page size (default: 100) */
11
+ defaultPageSize?: number;
12
+ /** Default sort order (default: asc) */
13
+ defaultSortOrder?: 'asc' | 'desc';
14
+ /** Default sort field (default: order) */
15
+ defaultSortField?: string;
16
+ /** Request timeout in milliseconds (default: 10000) */
17
+ timeout?: number;
18
+ }
19
+ interface PaginationOptions {
20
+ page?: number;
21
+ pageSize?: number;
22
+ }
23
+ interface SortOptions {
24
+ field?: string;
25
+ order?: 'asc' | 'desc';
26
+ }
27
+ interface FetchOptions {
28
+ /** Only fetch active items (default: true) */
29
+ activeOnly?: boolean;
30
+ pagination?: PaginationOptions;
31
+ sort?: SortOptions;
32
+ /** Override default fields */
33
+ fields?: string[];
34
+ /** Fetch all pages automatically */
35
+ fetchAll?: boolean;
36
+ }
37
+ interface MasterDataItem {
38
+ documentId: string;
39
+ key: string;
40
+ label: string;
41
+ order: number;
42
+ is_active: boolean;
43
+ [key: string]: unknown;
44
+ }
45
+ interface PaginationMeta {
46
+ page: number;
47
+ pageSize: number;
48
+ pageCount: number;
49
+ total: number;
50
+ }
51
+ interface MasterDataResponse {
52
+ data: MasterDataItem[];
53
+ meta: {
54
+ pagination: PaginationMeta;
55
+ };
56
+ }
57
+
58
+ declare class MasterDataClient {
59
+ private readonly config;
60
+ constructor(config: MasterDataClientConfig);
61
+ /**
62
+ * Fetch one page of master data items for the given group code.
63
+ */
64
+ fetch(groupCode: string, options?: FetchOptions): Promise<MasterDataResponse>;
65
+ /**
66
+ * Fetch ALL pages for the given group code and return a flat item array.
67
+ * Uses the `fetchAll` flag or call this method directly.
68
+ */
69
+ fetchAll(groupCode: string, options?: Omit<FetchOptions, 'fetchAll'>): Promise<MasterDataItem[]>;
70
+ /**
71
+ * Convenience: fetch items and return the data array (single page).
72
+ */
73
+ getItems(groupCode: string, options?: FetchOptions): Promise<MasterDataItem[]>;
74
+ /**
75
+ * Update client config at runtime (e.g. refresh token).
76
+ */
77
+ updateConfig(partial: Partial<MasterDataClientConfig>): void;
78
+ }
79
+
80
+ declare function createClient(config: MasterDataClientConfig): MasterDataClient;
81
+
82
+ export { type FetchOptions, MasterDataClient, type MasterDataClientConfig, type MasterDataItem, type MasterDataResponse, type PaginationMeta, type PaginationOptions, type SortOptions, createClient };
@@ -0,0 +1,82 @@
1
+ interface MasterDataClientConfig {
2
+ /** Base URL of the API endpoint (e.g. https://api.example.com) */
3
+ baseUrl: string;
4
+ /** Bearer token for authentication */
5
+ token: string;
6
+ /** API path prefix (default: /api/master-datas) */
7
+ apiPath?: string;
8
+ /** Default fields to fetch (default: documentId,key,label,order,is_active) */
9
+ defaultFields?: string[];
10
+ /** Default page size (default: 100) */
11
+ defaultPageSize?: number;
12
+ /** Default sort order (default: asc) */
13
+ defaultSortOrder?: 'asc' | 'desc';
14
+ /** Default sort field (default: order) */
15
+ defaultSortField?: string;
16
+ /** Request timeout in milliseconds (default: 10000) */
17
+ timeout?: number;
18
+ }
19
+ interface PaginationOptions {
20
+ page?: number;
21
+ pageSize?: number;
22
+ }
23
+ interface SortOptions {
24
+ field?: string;
25
+ order?: 'asc' | 'desc';
26
+ }
27
+ interface FetchOptions {
28
+ /** Only fetch active items (default: true) */
29
+ activeOnly?: boolean;
30
+ pagination?: PaginationOptions;
31
+ sort?: SortOptions;
32
+ /** Override default fields */
33
+ fields?: string[];
34
+ /** Fetch all pages automatically */
35
+ fetchAll?: boolean;
36
+ }
37
+ interface MasterDataItem {
38
+ documentId: string;
39
+ key: string;
40
+ label: string;
41
+ order: number;
42
+ is_active: boolean;
43
+ [key: string]: unknown;
44
+ }
45
+ interface PaginationMeta {
46
+ page: number;
47
+ pageSize: number;
48
+ pageCount: number;
49
+ total: number;
50
+ }
51
+ interface MasterDataResponse {
52
+ data: MasterDataItem[];
53
+ meta: {
54
+ pagination: PaginationMeta;
55
+ };
56
+ }
57
+
58
+ declare class MasterDataClient {
59
+ private readonly config;
60
+ constructor(config: MasterDataClientConfig);
61
+ /**
62
+ * Fetch one page of master data items for the given group code.
63
+ */
64
+ fetch(groupCode: string, options?: FetchOptions): Promise<MasterDataResponse>;
65
+ /**
66
+ * Fetch ALL pages for the given group code and return a flat item array.
67
+ * Uses the `fetchAll` flag or call this method directly.
68
+ */
69
+ fetchAll(groupCode: string, options?: Omit<FetchOptions, 'fetchAll'>): Promise<MasterDataItem[]>;
70
+ /**
71
+ * Convenience: fetch items and return the data array (single page).
72
+ */
73
+ getItems(groupCode: string, options?: FetchOptions): Promise<MasterDataItem[]>;
74
+ /**
75
+ * Update client config at runtime (e.g. refresh token).
76
+ */
77
+ updateConfig(partial: Partial<MasterDataClientConfig>): void;
78
+ }
79
+
80
+ declare function createClient(config: MasterDataClientConfig): MasterDataClient;
81
+
82
+ export { type FetchOptions, MasterDataClient, type MasterDataClientConfig, type MasterDataItem, type MasterDataResponse, type PaginationMeta, type PaginationOptions, type SortOptions, createClient };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ var m=["documentId","key","label","order","is_active"];var C="order";function D(a){return a.map(([t,e])=>`${encodeURIComponent(t).replace(/%5B/gi,"[").replace(/%5D/gi,"]").replace(/%24/gi,"$")}=${encodeURIComponent(e)}`).join("&")}function h(a,t,e){var d,f,p,g;let r=[],n=((d=t.sort)==null?void 0:d.field)??e.defaultSortField,o=((f=t.sort)==null?void 0:f.order)??e.defaultSortOrder;r.push([`sort[${n}]`,o]);let l=((p=t.pagination)==null?void 0:p.page)??1,i=((g=t.pagination)==null?void 0:g.pageSize)??e.defaultPageSize;r.push(["pagination[page]",String(l)]),r.push(["pagination[pageSize]",String(i)]);let s=t.fields??e.defaultFields;return r.push(["fields",s.join(",")]),r.push(["filters[$and][0][groups][code][$containsi]",a]),t.activeOnly!==false&&r.push(["filters[$and][1][is_active][$eq]","true"]),D(r)}function c(a){return {baseUrl:a.baseUrl.replace(/\/$/,""),token:a.token,apiPath:a.apiPath??"/api/master-datas",defaultFields:a.defaultFields??m,defaultPageSize:a.defaultPageSize??100,defaultSortOrder:a.defaultSortOrder??"asc",defaultSortField:a.defaultSortField??C,timeout:a.timeout??1e4}}var u=class{config;constructor(t){if(!t.baseUrl)throw new Error("[master-data] baseUrl is required");if(!t.token)throw new Error("[master-data] token is required");this.config=c(t);}async fetch(t,e={}){if(!t)throw new Error("[master-data] groupCode is required");let r=h(t,e,this.config),n=`${this.config.baseUrl}${this.config.apiPath}?${r}`,o=new AbortController,l=setTimeout(()=>o.abort(),this.config.timeout),i;try{i=await fetch(n,{method:"GET",headers:{Authorization:`Bearer ${this.config.token}`,"Content-Type":"application/json"},signal:o.signal});}catch(s){throw s.name==="AbortError"?new Error(`[master-data] Request timed out after ${this.config.timeout}ms`):s}finally{clearTimeout(l);}if(!i.ok){let s=await i.text().catch(()=>"");throw new Error(`[master-data] API error ${i.status} ${i.statusText}: ${s}`)}return i.json()}async fetchAll(t,e={}){var l;let r=((l=e.pagination)==null?void 0:l.pageSize)??this.config.defaultPageSize,n=1,o=[];for(;;){let i=await this.fetch(t,{...e,pagination:{page:n,pageSize:r}});o.push(...i.data);let{pageCount:s}=i.meta.pagination;if(n>=s)break;n++;}return o}async getItems(t,e={}){return e.fetchAll?this.fetchAll(t,e):(await this.fetch(t,e)).data}updateConfig(t){let e=c({...this.config,...t});Object.assign(this.config,e);}};function S(a){return new u(a)}export{u as MasterDataClient,S as createClient};
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@onagentic/master-data",
3
+ "version": "1.0.0",
4
+ "description": "Lightweight client for fetching master data from a Strapi-compatible API endpoint",
5
+ "keywords": [
6
+ "master-data",
7
+ "strapi",
8
+ "api-client"
9
+ ],
10
+ "license": "MIT",
11
+ "type": "module",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js",
16
+ "require": "./dist/index.cjs"
17
+ }
18
+ },
19
+ "main": "./dist/index.cjs",
20
+ "module": "./dist/index.js",
21
+ "types": "./dist/index.d.ts",
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "scripts": {
26
+ "build": "tsup",
27
+ "test": "node --experimental-vm-modules node_modules/.bin/jest",
28
+ "prepublishOnly": "npm run build && npm test"
29
+ },
30
+ "devDependencies": {
31
+ "@jest/globals": "^29.7.0",
32
+ "@types/node": "^22.0.0",
33
+ "jest": "^29.7.0",
34
+ "ts-jest": "^29.2.0",
35
+ "tsup": "^8.5.1",
36
+ "typescript": "^5.5.0"
37
+ },
38
+ "engines": {
39
+ "node": ">=18.0.0"
40
+ },
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "https://github.com/onagentic/master-data.git"
44
+ }
45
+ }