@kivacore/app-utils 0.0.0 → 0.22.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/README.md +1515 -1
- package/dist/index.d.ts +82 -0
- package/dist/index.js +2304 -0
- package/dist/index.js.map +1 -0
- package/dist/index.native.js +4695 -0
- package/dist/index.native.js.map +1 -0
- package/dist/query.d.ts +54 -0
- package/dist/query.js +269 -0
- package/dist/query.js.map +1 -0
- package/dist/types-GNkXRpu4.d.ts +754 -0
- package/package.json +57 -3
- package/src/app-config-client.ts +48 -0
- package/src/async-each-helper.ts +98 -0
- package/src/brand-client.ts +80 -0
- package/src/data-form-client.ts +85 -0
- package/src/data-source-client.ts +298 -0
- package/src/data-source-queries.ts +149 -0
- package/src/data-view-client.ts +92 -0
- package/src/date-utils.test.ts +48 -0
- package/src/date-utils.ts +146 -0
- package/src/envelope-interceptor.ts +65 -0
- package/src/get-tenant-preferences.test.ts +42 -0
- package/src/get-tenant-preferences.ts +30 -0
- package/src/handlebars-async-helpers.d.ts +12 -0
- package/src/index.native.ts +9 -0
- package/src/index.ts +88 -0
- package/src/inventory-client.ts +725 -0
- package/src/jsonata-helpers.ts +545 -0
- package/src/number-utils.ts +305 -0
- package/src/php-date-formatter.d.ts +8 -0
- package/src/query.ts +5 -0
- package/src/shims/stream.ts +11 -0
- package/src/template-engine.test.ts +47 -0
- package/src/template-engine.ts +298 -0
- package/src/types.ts +871 -0
- package/src/unwrap-envelope.ts +51 -0
- package/src/user-app-config-client.ts +44 -0
- package/src/user-identity-client.ts +21 -0
- package/tsconfig.json +9 -0
- package/tsup.config.ts +71 -0
- package/vitest.config.mts +13 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Top-level keys allowed to coexist with `data` in a Docyrus API envelope.
|
|
3
|
+
* Mirrors what the NestJS global ResponseInterceptor produces — every
|
|
4
|
+
* success response is shaped as `{ success, data, ...rest }`, optionally
|
|
5
|
+
* with `meta` (pagination) or `error` / `message` (deliberate
|
|
6
|
+
* controller-level error envelope).
|
|
7
|
+
*/
|
|
8
|
+
const ENVELOPE_KEYS = new Set([
|
|
9
|
+
'data',
|
|
10
|
+
'success',
|
|
11
|
+
'meta',
|
|
12
|
+
'error',
|
|
13
|
+
'message'
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Aggressively pull `.data` out of a Docyrus API envelope when the shape
|
|
18
|
+
* matches. Used inside app-utils' typed clients (e.g. `dataSources.list`,
|
|
19
|
+
* `dataViews.get`) which always return the inner payload — meta is
|
|
20
|
+
* intentionally discarded here because callers asked for a typed array
|
|
21
|
+
* or record, not the envelope.
|
|
22
|
+
*
|
|
23
|
+
* This is a wider rule than `@kivacore/api-client`'s envelope interceptor,
|
|
24
|
+
* which preserves envelopes carrying `meta` / `error` / `message` so
|
|
25
|
+
* callers (e.g. data grids) can read pagination/error info. App-utils
|
|
26
|
+
* clients don't need that — they have explicit return types and the
|
|
27
|
+
* sidecar info isn't part of the contract — so they extract regardless.
|
|
28
|
+
*
|
|
29
|
+
* The closed-set check still protects domain payloads like AppConfig /
|
|
30
|
+
* UserAppConfig (records that carry a `data` column) from being falsely
|
|
31
|
+
* unwrapped, because they also carry domain fields (`id`, `status`,
|
|
32
|
+
* `tenant_app_id`, …) that aren't in the envelope set.
|
|
33
|
+
*/
|
|
34
|
+
export function unwrapEnvelope<T>(response: unknown): T {
|
|
35
|
+
if (
|
|
36
|
+
response !== null
|
|
37
|
+
&& typeof response === 'object'
|
|
38
|
+
&& !Array.isArray(response)
|
|
39
|
+
) {
|
|
40
|
+
const keys = Object.keys(response);
|
|
41
|
+
const looksLikeEnvelope
|
|
42
|
+
= 'data' in response
|
|
43
|
+
&& keys.every(key => ENVELOPE_KEYS.has(key));
|
|
44
|
+
|
|
45
|
+
if (looksLikeEnvelope) {
|
|
46
|
+
return (response as { data: T }).data;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return response as T;
|
|
51
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type RestApiClient } from '@kivacore/api-client';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type CreateUserAppConfigClientOptions,
|
|
5
|
+
type InventoryClient,
|
|
6
|
+
type UpsertUserAppConfigBody,
|
|
7
|
+
type UserAppConfig,
|
|
8
|
+
type UserAppConfigClient
|
|
9
|
+
} from './types';
|
|
10
|
+
import { unwrapEnvelope } from './unwrap-envelope';
|
|
11
|
+
|
|
12
|
+
export function createUserAppConfigClient(
|
|
13
|
+
client: RestApiClient,
|
|
14
|
+
appId: string,
|
|
15
|
+
options?: CreateUserAppConfigClientOptions
|
|
16
|
+
): UserAppConfigClient {
|
|
17
|
+
const basePath = `/v1/tenant/apps/${appId}/user-config`;
|
|
18
|
+
const inventory: InventoryClient | undefined = options?.inventory;
|
|
19
|
+
|
|
20
|
+
async function get(): Promise<UserAppConfig> {
|
|
21
|
+
if (inventory) {
|
|
22
|
+
const cached = await inventory.getUserAppConfig(appId);
|
|
23
|
+
|
|
24
|
+
if (cached) return cached;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return unwrapEnvelope<UserAppConfig>(await client.get(basePath));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function upsert(body: UpsertUserAppConfigBody): Promise<UserAppConfig> {
|
|
31
|
+
const result = unwrapEnvelope<UserAppConfig>(await client.put(basePath, body));
|
|
32
|
+
|
|
33
|
+
inventory?.setUserAppConfig(appId, result);
|
|
34
|
+
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function remove(): Promise<void> {
|
|
39
|
+
await client.delete(basePath);
|
|
40
|
+
inventory?.setUserAppConfig(appId, null);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return { get, upsert, remove };
|
|
44
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type RestApiClient } from '@kivacore/api-client';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type GoogleIdentityPayload,
|
|
5
|
+
type MicrosoftIdentityPayload,
|
|
6
|
+
type UserIdentityClient
|
|
7
|
+
} from './types';
|
|
8
|
+
|
|
9
|
+
export function createUserIdentityClient(client: RestApiClient): UserIdentityClient {
|
|
10
|
+
const basePath = '/v1/users/me/identity';
|
|
11
|
+
|
|
12
|
+
async function saveMicrosoft(payload: MicrosoftIdentityPayload): Promise<void> {
|
|
13
|
+
await client.put(`${basePath}/microsoft`, payload);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function saveGoogle(payload: GoogleIdentityPayload): Promise<void> {
|
|
17
|
+
await client.put(`${basePath}/google`, payload);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return { saveMicrosoft, saveGoogle };
|
|
21
|
+
}
|
package/tsconfig.json
ADDED
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url';
|
|
2
|
+
|
|
3
|
+
import { defineConfig } from 'tsup';
|
|
4
|
+
|
|
5
|
+
const external = [
|
|
6
|
+
'handlebars',
|
|
7
|
+
'jsonata',
|
|
8
|
+
'date-fns',
|
|
9
|
+
'@tanstack/query-core'
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
const streamShim = fileURLToPath(new URL('./src/shims/stream.ts', import.meta.url));
|
|
13
|
+
|
|
14
|
+
export default defineConfig([
|
|
15
|
+
{
|
|
16
|
+
entry: ['src/index.ts', 'src/query.ts'],
|
|
17
|
+
format: ['esm'],
|
|
18
|
+
dts: true,
|
|
19
|
+
splitting: false,
|
|
20
|
+
sourcemap: true,
|
|
21
|
+
clean: true,
|
|
22
|
+
treeshake: true,
|
|
23
|
+
minify: false,
|
|
24
|
+
external,
|
|
25
|
+
/*
|
|
26
|
+
* Bundle `handlebars-async-helpers` with its Node-only `stream` import
|
|
27
|
+
* redirected to the shim (as the React Native build does). Consumers then
|
|
28
|
+
* never resolve a Node core module, and its exact `handlebars@4.7.6` peer
|
|
29
|
+
* no longer forces a second Handlebars copy into their dependency tree.
|
|
30
|
+
*/
|
|
31
|
+
noExternal: ['handlebars-async-helpers'],
|
|
32
|
+
esbuildOptions(options) {
|
|
33
|
+
options.alias = { ...options.alias, stream: streamShim };
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
/*
|
|
38
|
+
* React Native build: bundle the two dependencies that pull Node core
|
|
39
|
+
* modules and point them at RN-safe code — `stream` → a tiny shim, and
|
|
40
|
+
* `jsonpath-plus` → its browser build (the node build imports `vm`).
|
|
41
|
+
*/
|
|
42
|
+
entry: { 'index.native': 'src/index.native.ts' },
|
|
43
|
+
format: ['esm'],
|
|
44
|
+
dts: false,
|
|
45
|
+
splitting: false,
|
|
46
|
+
sourcemap: true,
|
|
47
|
+
clean: false,
|
|
48
|
+
treeshake: true,
|
|
49
|
+
minify: false,
|
|
50
|
+
platform: 'neutral',
|
|
51
|
+
external,
|
|
52
|
+
noExternal: [
|
|
53
|
+
'handlebars-async-helpers',
|
|
54
|
+
'jsonpath-plus',
|
|
55
|
+
'php-date-formatter'
|
|
56
|
+
],
|
|
57
|
+
esbuildOptions(options) {
|
|
58
|
+
options.alias = { ...options.alias, stream: streamShim };
|
|
59
|
+
options.mainFields = [
|
|
60
|
+
'browser',
|
|
61
|
+
'module',
|
|
62
|
+
'main'
|
|
63
|
+
];
|
|
64
|
+
options.conditions = [
|
|
65
|
+
'browser',
|
|
66
|
+
'import',
|
|
67
|
+
'default'
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
]);
|