@lasterp/shared 1.0.0-alpha.2 → 1.0.0-alpha.3

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.
@@ -0,0 +1,98 @@
1
+ interface Item {
2
+ itemCode: string;
3
+ area: string;
4
+ modelNumber: string;
5
+ region: string;
6
+ grade: string;
7
+ gradeIssuer: string;
8
+ color: string;
9
+ storage: string;
10
+ memory: string;
11
+ connectivity: string;
12
+ carrierCompatibility: string;
13
+ }
14
+ interface ItemVariant extends Item {
15
+ itemVariant: string;
16
+ }
17
+ interface ModelNumber {
18
+ modelNumber: string;
19
+ simCardType: string;
20
+ connectivity: string;
21
+ }
22
+ interface Colour {
23
+ color: string;
24
+ hex: string;
25
+ colorSystem: string;
26
+ }
27
+ interface Globals {
28
+ header: Header;
29
+ footer: Footer;
30
+ }
31
+ interface Brand {
32
+ brandImage: string;
33
+ }
34
+ interface NavbarItem {
35
+ label: string;
36
+ enableDropdown: boolean;
37
+ enableLink: boolean;
38
+ link: string;
39
+ dropdownDescription: string;
40
+ groups: NavbarSubItemGroup[];
41
+ }
42
+ interface NavbarSubItemGroup {
43
+ title: string;
44
+ items: NavbarSubItem[];
45
+ }
46
+ interface NavbarSubItem {
47
+ label: string;
48
+ description: string;
49
+ image: string;
50
+ link: string;
51
+ }
52
+ interface Topbar {
53
+ topbarEnabled: boolean;
54
+ items: TopbarItem[];
55
+ }
56
+ interface TopbarItem {
57
+ icon: string;
58
+ label: string;
59
+ link: string;
60
+ }
61
+ interface Header {
62
+ brand: Brand;
63
+ headerType: string;
64
+ tabs: NavbarItem[];
65
+ topbar: Topbar;
66
+ }
67
+ interface FooterItemGroup {
68
+ title: string;
69
+ items: FooterItem[];
70
+ }
71
+ interface FooterItem {
72
+ label: string;
73
+ link: string;
74
+ }
75
+ interface Footer {
76
+ footerType: string;
77
+ groups: FooterItemGroup[];
78
+ copyright: string;
79
+ address: string;
80
+ country: string;
81
+ phone: string;
82
+ email: string;
83
+ }
84
+ interface Hero {
85
+ type: string;
86
+ data: Record<string, unknown>;
87
+ }
88
+ interface Block {
89
+ type: string;
90
+ data: Record<string, unknown>;
91
+ }
92
+ interface Page {
93
+ slug: string;
94
+ hero: Hero;
95
+ blocks: Block[];
96
+ }
97
+ declare function equalsIgnoreCase(str1: string, str2: string): boolean;
98
+ export { equalsIgnoreCase, TopbarItem, Topbar, Page, NavbarSubItemGroup, NavbarSubItem, NavbarItem, ModelNumber, ItemVariant, Item, Hero, Header, Globals, FooterItemGroup, FooterItem, Footer, Colour, Brand, Block };
@@ -0,0 +1,7 @@
1
+ // src/utils/char.ts
2
+ function equalsIgnoreCase(str1, str2) {
3
+ return str1.localeCompare(str2, undefined, { sensitivity: "accent" }) === 0;
4
+ }
5
+ export {
6
+ equalsIgnoreCase
7
+ };
@@ -0,0 +1,98 @@
1
+ interface Item {
2
+ itemCode: string;
3
+ area: string;
4
+ modelNumber: string;
5
+ region: string;
6
+ grade: string;
7
+ gradeIssuer: string;
8
+ color: string;
9
+ storage: string;
10
+ memory: string;
11
+ connectivity: string;
12
+ carrierCompatibility: string;
13
+ }
14
+ interface ItemVariant extends Item {
15
+ itemVariant: string;
16
+ }
17
+ interface ModelNumber {
18
+ modelNumber: string;
19
+ simCardType: string;
20
+ connectivity: string;
21
+ }
22
+ interface Colour {
23
+ color: string;
24
+ hex: string;
25
+ colorSystem: string;
26
+ }
27
+ interface Globals {
28
+ header: Header;
29
+ footer: Footer;
30
+ }
31
+ interface Brand {
32
+ brandImage: string;
33
+ }
34
+ interface NavbarItem {
35
+ label: string;
36
+ enableDropdown: boolean;
37
+ enableLink: boolean;
38
+ link: string;
39
+ dropdownDescription: string;
40
+ groups: NavbarSubItemGroup[];
41
+ }
42
+ interface NavbarSubItemGroup {
43
+ title: string;
44
+ items: NavbarSubItem[];
45
+ }
46
+ interface NavbarSubItem {
47
+ label: string;
48
+ description: string;
49
+ image: string;
50
+ link: string;
51
+ }
52
+ interface Topbar {
53
+ topbarEnabled: boolean;
54
+ items: TopbarItem[];
55
+ }
56
+ interface TopbarItem {
57
+ icon: string;
58
+ label: string;
59
+ link: string;
60
+ }
61
+ interface Header {
62
+ brand: Brand;
63
+ headerType: string;
64
+ tabs: NavbarItem[];
65
+ topbar: Topbar;
66
+ }
67
+ interface FooterItemGroup {
68
+ title: string;
69
+ items: FooterItem[];
70
+ }
71
+ interface FooterItem {
72
+ label: string;
73
+ link: string;
74
+ }
75
+ interface Footer {
76
+ footerType: string;
77
+ groups: FooterItemGroup[];
78
+ copyright: string;
79
+ address: string;
80
+ country: string;
81
+ phone: string;
82
+ email: string;
83
+ }
84
+ interface Hero {
85
+ type: string;
86
+ data: Record<string, unknown>;
87
+ }
88
+ interface Block {
89
+ type: string;
90
+ data: Record<string, unknown>;
91
+ }
92
+ interface Page {
93
+ slug: string;
94
+ hero: Hero;
95
+ blocks: Block[];
96
+ }
97
+ declare function equalsIgnoreCase(str1: string, str2: string): boolean;
98
+ export { equalsIgnoreCase, TopbarItem, Topbar, Page, NavbarSubItemGroup, NavbarSubItem, NavbarItem, ModelNumber, ItemVariant, Item, Hero, Header, Globals, FooterItemGroup, FooterItem, Footer, Colour, Brand, Block };
@@ -0,0 +1,7 @@
1
+ // src/utils/char.ts
2
+ function equalsIgnoreCase(str1, str2) {
3
+ return str1.localeCompare(str2, undefined, { sensitivity: "accent" }) === 0;
4
+ }
5
+ export {
6
+ equalsIgnoreCase
7
+ };
package/package.json CHANGED
@@ -1,49 +1,40 @@
1
1
  {
2
2
  "name": "@lasterp/shared",
3
- "version": "1.0.0-alpha.2",
4
- "type": "module",
5
- "description": "Shared types and business logic for LastERP Web and Native apps",
6
- "main": "./dist/index.cjs",
7
- "module": "./dist/index.js",
8
- "types": "./dist/index.d.ts",
9
- "exports": {
10
- ".": {
11
- "types": "./dist/index.d.ts",
12
- "import": "./dist/index.js",
13
- "require": "./dist/index.cjs"
14
- }
15
- },
3
+ "version": "1.0.0-alpha.3",
4
+ "description": "Shared repo for webapp and native app",
5
+ "license": "MIT",
16
6
  "files": [
17
- "dist",
18
- "src"
7
+ "dist"
19
8
  ],
20
9
  "scripts": {
21
- "build": "tsup",
22
- "dev": "tsup --watch --onSuccess \"yalc push\"",
23
- "type-check": "tsc --noEmit",
24
- "dev:publish": "pnpm run build && yalc publish",
25
- "dev:push": "pnpm run build && yalc push"
10
+ "build": "bunup",
11
+ "dev": "bunup --watch",
12
+ "type-check": "tsc --noEmit"
13
+ },
14
+ "devDependencies": {
15
+ "@types/bun": "^1.3.9",
16
+ "bunup": "^0.16.26",
17
+ "typescript": "^5.9.3"
26
18
  },
27
- "keywords": [
28
- "lasterp",
29
- "shared",
30
- "typescript",
31
- "business-logic"
32
- ],
33
- "author": "",
34
- "license": "ISC",
35
- "packageManager": "pnpm@10.28.1",
36
19
  "peerDependencies": {
37
- "@tanstack/react-query": "^5.0.0",
38
- "react": "^18.0.0 || ^19.0.0"
20
+ "typescript": ">=4.5.0"
39
21
  },
40
- "devDependencies": {
41
- "@types/humps": "^2.0.6",
42
- "@types/react": "^19",
43
- "tsup": "^8.5.1",
44
- "typescript": "^5.9.3",
45
- "yalc": "1.0.0-pre.53"
22
+ "peerDependenciesMeta": {
23
+ "typescript": {
24
+ "optional": true
25
+ }
46
26
  },
27
+ "type": "module",
28
+ "exports": {
29
+ ".": {
30
+ "node": "./dist/node/index.js",
31
+ "rn": "./dist/rn/index.js",
32
+ "default": "./dist/node/index.js"
33
+ },
34
+ "./package.json": "./package.json"
35
+ },
36
+ "module": "./dist/index.js",
37
+ "types": "./dist/index.d.ts",
47
38
  "dependencies": {
48
39
  "humps": "^2.0.1"
49
40
  }
package/dist/index.cjs DELETED
@@ -1,386 +0,0 @@
1
- 'use strict';
2
-
3
- var humps = require('humps');
4
- var react = require('react');
5
- var jsxRuntime = require('react/jsx-runtime');
6
- var reactQuery = require('@tanstack/react-query');
7
-
8
- async function request(baseUrl, endpoint, options = {}, auth) {
9
- const url = `${baseUrl}${endpoint}`;
10
- const headers = {
11
- "Content-Type": "application/json",
12
- Accept: "application/json",
13
- ...options.headers
14
- };
15
- if (auth?.token) {
16
- headers["Authorization"] = `Bearer ${auth.token}`;
17
- } else if (auth?.apiKey) {
18
- headers["Authorization"] = `token ${auth.apiKey.key}:${auth.apiKey.secret}`;
19
- }
20
- const response = await fetch(url, {
21
- ...options,
22
- headers,
23
- credentials: "include"
24
- });
25
- const rawData = await response.json();
26
- if (!response.ok || rawData.exc) {
27
- const errorMessage = rawData._error_message || rawData.exc || "Request failed";
28
- const error = new Error(errorMessage);
29
- error.message = errorMessage;
30
- error.statusCode = response.status;
31
- if (rawData.exc) {
32
- error.exc = rawData.exc;
33
- }
34
- if (rawData._server_messages) {
35
- error.serverMessages = rawData._server_messages;
36
- }
37
- if (rawData.exc_type) {
38
- error.excType = rawData.exc_type;
39
- }
40
- throw error;
41
- }
42
- const result = rawData.message ?? rawData;
43
- return humps.camelizeKeys(result);
44
- }
45
- async function frappeCall(baseUrl, method, args, auth) {
46
- const snakeArgs = humps.decamelizeKeys(args ?? {});
47
- return request(
48
- baseUrl,
49
- `/api/method/${method}`,
50
- {
51
- method: "POST",
52
- body: JSON.stringify(snakeArgs)
53
- },
54
- auth
55
- );
56
- }
57
- async function frappeCallGet(baseUrl, method, args, auth) {
58
- const snakeArgs = humps.decamelizeKeys(args ?? {});
59
- const queryParams = new URLSearchParams();
60
- Object.entries(snakeArgs).forEach(([key, value]) => {
61
- queryParams.append(
62
- key,
63
- typeof value === "string" ? value : JSON.stringify(value)
64
- );
65
- });
66
- const endpoint = `/api/method/${method}?${queryParams.toString()}`;
67
- return request(baseUrl, endpoint, { method: "GET" }, auth);
68
- }
69
- async function getList(baseUrl, params, auth) {
70
- const {
71
- doctype,
72
- fields = ["name"],
73
- filters = {},
74
- orderBy = "modified desc",
75
- limitStart = 0,
76
- limitPageLength = 20
77
- } = params;
78
- const snakeFilters = humps.decamelizeKeys(filters);
79
- const queryParams = new URLSearchParams({
80
- fields: JSON.stringify(fields),
81
- filters: JSON.stringify(snakeFilters),
82
- order_by: orderBy,
83
- limit_start: limitStart.toString(),
84
- limit_page_length: limitPageLength.toString()
85
- });
86
- const endpoint = `/api/resource/${doctype}?${queryParams.toString()}`;
87
- const response = await request(baseUrl, endpoint, {}, auth);
88
- return response.data || [];
89
- }
90
- async function getDoc(baseUrl, doctype, name, auth) {
91
- const endpoint = `/api/resource/${doctype}/${encodeURIComponent(name)}`;
92
- const response = await request(baseUrl, endpoint, {}, auth);
93
- return response.data;
94
- }
95
- async function createDoc(baseUrl, doctype, doc, auth) {
96
- const snakeDoc = humps.decamelizeKeys(doc);
97
- const endpoint = `/api/resource/${doctype}`;
98
- const response = await request(
99
- baseUrl,
100
- endpoint,
101
- {
102
- method: "POST",
103
- body: JSON.stringify(snakeDoc)
104
- },
105
- auth
106
- );
107
- return response.data;
108
- }
109
- async function updateDoc(baseUrl, doctype, name, doc, auth) {
110
- const snakeDoc = humps.decamelizeKeys(doc);
111
- const endpoint = `/api/resource/${doctype}/${encodeURIComponent(name)}`;
112
- const response = await request(
113
- baseUrl,
114
- endpoint,
115
- {
116
- method: "PUT",
117
- body: JSON.stringify(snakeDoc)
118
- },
119
- auth
120
- );
121
- return response.data;
122
- }
123
- async function deleteDoc(baseUrl, doctype, name, auth) {
124
- const endpoint = `/api/resource/${doctype}/${encodeURIComponent(name)}`;
125
- await request(
126
- baseUrl,
127
- endpoint,
128
- {
129
- method: "DELETE"
130
- },
131
- auth
132
- );
133
- }
134
- async function getValue(baseUrl, doctype, name, fieldname, auth) {
135
- const endpoint = `/api/resource/${doctype}/${encodeURIComponent(name)}?fields=["${fieldname}"]`;
136
- const response = await request(
137
- baseUrl,
138
- endpoint,
139
- {},
140
- auth
141
- );
142
- const value = response.data[fieldname];
143
- if (value === void 0) {
144
- throw new Error(
145
- `Field "${fieldname}" not found in document "${doctype}/${name}"`
146
- );
147
- }
148
- return value;
149
- }
150
- async function setValue(baseUrl, doctype, name, fieldname, value, auth) {
151
- return updateDoc(baseUrl, doctype, name, { [fieldname]: value }, auth);
152
- }
153
- async function getCount(baseUrl, doctype, filters = {}, auth) {
154
- return frappeCall(
155
- baseUrl,
156
- "frappe.client.get_count",
157
- {
158
- doctype,
159
- filters
160
- },
161
- auth
162
- );
163
- }
164
- async function getCurrentUser(baseUrl, auth) {
165
- return frappeCallGet(baseUrl, "frappe.auth.get_logged_user", {}, auth);
166
- }
167
- var FrappeContext = react.createContext(null);
168
- function FrappeProvider({
169
- baseUrl,
170
- token: initialToken,
171
- apiKey: initialApiKey,
172
- autoLoadAuth = true,
173
- onLoadAuth,
174
- children
175
- }) {
176
- const [token, setToken2] = react.useState(initialToken);
177
- const [apiKey, setApiKey2] = react.useState(
178
- initialApiKey
179
- );
180
- react.useEffect(() => {
181
- if (autoLoadAuth && !initialToken && !initialApiKey && onLoadAuth) {
182
- Promise.resolve(onLoadAuth()).then((auth) => {
183
- if (auth.token) {
184
- setToken2(auth.token);
185
- } else if (auth.apiKey) {
186
- setApiKey2(auth.apiKey);
187
- }
188
- });
189
- }
190
- }, [autoLoadAuth, initialToken, initialApiKey, onLoadAuth]);
191
- const value = {
192
- baseUrl,
193
- ...token !== void 0 && { token },
194
- ...apiKey !== void 0 && { apiKey }
195
- };
196
- return /* @__PURE__ */ jsxRuntime.jsx(FrappeContext.Provider, { value, children });
197
- }
198
- function useFrappeConfig() {
199
- const context = react.useContext(FrappeContext);
200
- if (!context) {
201
- throw new Error("useFrappeConfig must be used within FrappeProvider");
202
- }
203
- return context;
204
- }
205
-
206
- // src/client/hooks.tsx
207
- function useFrappe() {
208
- const { baseUrl, token, apiKey } = useFrappeConfig();
209
- const auth = {
210
- ...token && { token },
211
- ...apiKey && { apiKey }
212
- };
213
- return {
214
- call: (method, args) => frappeCall(baseUrl, method, args, auth),
215
- callGet: (method, args) => frappeCallGet(baseUrl, method, args, auth),
216
- getList: (params) => getList(baseUrl, params, auth),
217
- getDoc: (doctype, name) => getDoc(baseUrl, doctype, name, auth),
218
- createDoc: (doctype, doc) => createDoc(baseUrl, doctype, doc, auth),
219
- updateDoc: (doctype, name, doc) => updateDoc(baseUrl, doctype, name, doc, auth),
220
- deleteDoc: (doctype, name) => deleteDoc(baseUrl, doctype, name, auth),
221
- baseUrl,
222
- auth
223
- };
224
- }
225
-
226
- // src/client/storage.ts
227
- var webStorageAdapter = {
228
- getItem: (key) => {
229
- if (typeof window === "undefined") return null;
230
- return localStorage.getItem(key);
231
- },
232
- setItem: (key, value) => {
233
- if (typeof window === "undefined") return;
234
- localStorage.setItem(key, value);
235
- },
236
- removeItem: (key) => {
237
- if (typeof window === "undefined") return;
238
- localStorage.removeItem(key);
239
- }
240
- };
241
- var currentAdapter = webStorageAdapter;
242
- function setStorageAdapter(adapter) {
243
- currentAdapter = adapter;
244
- }
245
- function getStorageAdapter() {
246
- return currentAdapter;
247
- }
248
- var TOKEN_KEY = "frappe_token";
249
- var API_KEY_KEY = "frappe_api_key";
250
- var API_SECRET_KEY = "frappe_api_secret";
251
- function getToken() {
252
- return currentAdapter.getItem(TOKEN_KEY);
253
- }
254
- function setToken(token) {
255
- return currentAdapter.setItem(TOKEN_KEY, token);
256
- }
257
- function clearToken() {
258
- return currentAdapter.removeItem(TOKEN_KEY);
259
- }
260
- async function getApiKey() {
261
- const key = await currentAdapter.getItem(API_KEY_KEY);
262
- const secret = await currentAdapter.getItem(API_SECRET_KEY);
263
- if (!key || !secret) return null;
264
- return { key, secret };
265
- }
266
- async function setApiKey(key, secret) {
267
- await currentAdapter.setItem(API_KEY_KEY, key);
268
- await currentAdapter.setItem(API_SECRET_KEY, secret);
269
- }
270
- async function clearApiKey() {
271
- await currentAdapter.removeItem(API_KEY_KEY);
272
- await currentAdapter.removeItem(API_SECRET_KEY);
273
- }
274
- async function clearAuth() {
275
- await clearToken();
276
- await clearApiKey();
277
- }
278
- async function isAuthenticated() {
279
- const token = await getToken();
280
- const apiKey = await getApiKey();
281
- return !!token || !!apiKey;
282
- }
283
-
284
- // src/design/page/api.ts
285
- async function getPage(baseUrl, slug, auth) {
286
- return getDoc(baseUrl, "Design Page", slug, auth);
287
- }
288
-
289
- // src/utils/catalog.ts
290
- function toDescription(modelNumber) {
291
- if (!modelNumber) {
292
- return null;
293
- }
294
- return modelNumber.simCardType;
295
- }
296
-
297
- // src/utils/types.ts
298
- function equalsIgnoreCase(str1, str2) {
299
- return str1.localeCompare(str2, void 0, { sensitivity: "accent" }) === 0;
300
- }
301
-
302
- // src/lasterp/shop/api.ts
303
- async function getShopContext(baseUrl, params, auth) {
304
- return frappeCall(
305
- baseUrl,
306
- "lasterp.shop.controllers.shop_controller.get_context",
307
- params,
308
- auth
309
- );
310
- }
311
- async function getProductContext(baseUrl, productName, auth) {
312
- return frappeCall(
313
- baseUrl,
314
- "lasterp.shop.controllers.product_controller.get_context",
315
- { productName },
316
- auth
317
- );
318
- }
319
- function useShopContext(params, options) {
320
- const { baseUrl, auth } = useFrappe();
321
- return reactQuery.useQuery({
322
- queryKey: ["shop-context", params],
323
- queryFn: () => getShopContext(baseUrl, params, auth),
324
- ...options
325
- });
326
- }
327
- function useProductContext(productName, options) {
328
- const { baseUrl, auth } = useFrappe();
329
- return reactQuery.useQuery({
330
- queryKey: ["product-context", productName],
331
- queryFn: () => getProductContext(baseUrl, productName, auth),
332
- enabled: !!productName,
333
- ...options
334
- });
335
- }
336
-
337
- Object.defineProperty(exports, "camelToSnake", {
338
- enumerable: true,
339
- get: function () { return humps.decamelize; }
340
- });
341
- Object.defineProperty(exports, "objectCamelToSnake", {
342
- enumerable: true,
343
- get: function () { return humps.decamelizeKeys; }
344
- });
345
- Object.defineProperty(exports, "objectSnakeToCamel", {
346
- enumerable: true,
347
- get: function () { return humps.camelizeKeys; }
348
- });
349
- Object.defineProperty(exports, "snakeToCamel", {
350
- enumerable: true,
351
- get: function () { return humps.camelize; }
352
- });
353
- exports.FrappeProvider = FrappeProvider;
354
- exports.clearApiKey = clearApiKey;
355
- exports.clearAuth = clearAuth;
356
- exports.clearToken = clearToken;
357
- exports.createDoc = createDoc;
358
- exports.deleteDoc = deleteDoc;
359
- exports.equalsIgnoreCase = equalsIgnoreCase;
360
- exports.frappeCall = frappeCall;
361
- exports.frappeCallGet = frappeCallGet;
362
- exports.getApiKey = getApiKey;
363
- exports.getCount = getCount;
364
- exports.getCurrentUser = getCurrentUser;
365
- exports.getDoc = getDoc;
366
- exports.getList = getList;
367
- exports.getPage = getPage;
368
- exports.getProductContext = getProductContext;
369
- exports.getShopContext = getShopContext;
370
- exports.getStorageAdapter = getStorageAdapter;
371
- exports.getToken = getToken;
372
- exports.getValue = getValue;
373
- exports.isAuthenticated = isAuthenticated;
374
- exports.setApiKey = setApiKey;
375
- exports.setStorageAdapter = setStorageAdapter;
376
- exports.setToken = setToken;
377
- exports.setValue = setValue;
378
- exports.toDescription = toDescription;
379
- exports.updateDoc = updateDoc;
380
- exports.useFrappe = useFrappe;
381
- exports.useFrappeConfig = useFrappeConfig;
382
- exports.useProductContext = useProductContext;
383
- exports.useShopContext = useShopContext;
384
- exports.webStorageAdapter = webStorageAdapter;
385
- //# sourceMappingURL=index.cjs.map
386
- //# sourceMappingURL=index.cjs.map