@junobuild/functions 0.0.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/LICENSE +21 -0
- package/README.md +21 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/index.js +1 -0
- package/dist/types/configs/assert.config.d.ts +30 -0
- package/dist/types/configs/collection.config.d.ts +9 -0
- package/dist/types/configs/hook.config.d.ts +28 -0
- package/dist/types/configs/satellite.config.d.ts +8 -0
- package/dist/types/global.d.ts +6 -0
- package/dist/types/hooks/context.d.ts +50 -0
- package/dist/types/hooks/core.d.ts +30 -0
- package/dist/types/hooks/datastore.d.ts +85 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/sdk/datastore.sdk.d.ts +3 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 David Dal Busco
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[![npm][npm-badge]][npm-badge-url]
|
|
2
|
+
[![license][npm-license]][npm-license-url]
|
|
3
|
+
|
|
4
|
+
[npm-badge]: https://img.shields.io/npm/v/@junobuild/functions
|
|
5
|
+
[npm-badge-url]: https://www.npmjs.com/package/@junobuild/functions
|
|
6
|
+
[npm-license]: https://img.shields.io/npm/l/@junobuild/functions
|
|
7
|
+
[npm-license-url]: https://github.com/junobuild/juno-js/blob/main/LICENSE
|
|
8
|
+
|
|
9
|
+
# Juno Config
|
|
10
|
+
|
|
11
|
+
JavaScript and TypeScript utilities for [Juno] Serverless Functions.
|
|
12
|
+
|
|
13
|
+
<!-- TSDOC_START -->
|
|
14
|
+
|
|
15
|
+
<!-- TSDOC_END -->
|
|
16
|
+
|
|
17
|
+
## License
|
|
18
|
+
|
|
19
|
+
MIT © [David Dal Busco](mailto:david.dalbusco@outlook.com)
|
|
20
|
+
|
|
21
|
+
[juno]: https://juno.build
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{jsonReplacer as t,jsonReviver as e}from"@dfinity/utils";var a=o=>JSON.parse(__juno_satellite_datastore_raw_data_to_text(o),e),p=o=>__juno_satellite_datastore_raw_data_from_text(JSON.stringify(o,t));export{a as decodeDocData,p as encodeDocData};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/sdk/datastore.sdk.ts"],
|
|
4
|
+
"sourcesContent": ["import {jsonReplacer, jsonReviver} from '@dfinity/utils';\nimport type {RawData} from '../hooks/core';\n\nexport const decodeDocData = <T>(data: RawData): T =>\n JSON.parse(__juno_satellite_datastore_raw_data_to_text(data), jsonReviver);\n\nexport const encodeDocData = <T>(data: T): RawData =>\n __juno_satellite_datastore_raw_data_from_text(JSON.stringify(data, jsonReplacer));\n"],
|
|
5
|
+
"mappings": "AAAA,OAAQ,gBAAAA,EAAc,eAAAC,MAAkB,iBAGjC,IAAMC,EAAoBC,GAC/B,KAAK,MAAM,4CAA4CA,CAAI,EAAGF,CAAW,EAE9DG,EAAoBD,GAC/B,8CAA8C,KAAK,UAAUA,EAAMH,CAAY,CAAC",
|
|
6
|
+
"names": ["jsonReplacer", "jsonReviver", "decodeDocData", "data", "encodeDocData"]
|
|
7
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './browser/index.js';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { AssertSetDocContext } from '../hooks/context';
|
|
2
|
+
import type { CollectionsConfig } from './collection.config';
|
|
3
|
+
import type { SatelliteConfigEnv } from './satellite.config';
|
|
4
|
+
/**
|
|
5
|
+
* The generic configuration for assertion hooks that manage collections.
|
|
6
|
+
*/
|
|
7
|
+
export interface AssertAssertConfig extends CollectionsConfig {
|
|
8
|
+
assertSetDoc: never;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A configuration object that includes the `assertSetDoc` function.
|
|
12
|
+
* This function is called to validate a document before it is created or updated.
|
|
13
|
+
*/
|
|
14
|
+
export interface AssertSetDocConfig extends Omit<AssertAssertConfig, 'assertSetDoc'> {
|
|
15
|
+
/**
|
|
16
|
+
* A function that runs synchronously before a document is set in the Datastore.
|
|
17
|
+
* This can be used to enforce your validation rules.
|
|
18
|
+
*
|
|
19
|
+
* @param {AssertSetDocContext} context - Provides details about the document being validated.
|
|
20
|
+
* @throws {Error} If your validation fails, throw an exception to prevent the document from being saved.
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
assertSetDoc: (context: AssertSetDocContext) => void;
|
|
24
|
+
}
|
|
25
|
+
export type AssertConfig = AssertSetDocConfig;
|
|
26
|
+
export type AssertFn = (config: SatelliteConfigEnv) => AssertConfig;
|
|
27
|
+
export type AssertFnOrObject = AssertConfig | AssertFn;
|
|
28
|
+
export declare function defineAssert(config: AssertConfig): AssertConfig;
|
|
29
|
+
export declare function defineAssert(config: AssertFn): AssertFn;
|
|
30
|
+
export declare function defineAssert(config: AssertFnOrObject): AssertFnOrObject;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { OnSetDocContext } from '../hooks/context';
|
|
2
|
+
import type { CollectionsConfig } from './collection.config';
|
|
3
|
+
import type { SatelliteConfigEnv } from './satellite.config';
|
|
4
|
+
/**
|
|
5
|
+
* The generic configuration for hooks that manage collections.
|
|
6
|
+
*/
|
|
7
|
+
export interface OnHookConfig extends CollectionsConfig {
|
|
8
|
+
onSetDoc: never;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A configuration object that includes the `onSetDoc` function.
|
|
12
|
+
* This function is called when a document is created or updated.
|
|
13
|
+
*/
|
|
14
|
+
export interface OnSetDocConfig extends Omit<OnHookConfig, 'onSetDoc'> {
|
|
15
|
+
/**
|
|
16
|
+
* A function that runs when a document is set in the Datastore.
|
|
17
|
+
*
|
|
18
|
+
* @param {OnSetDocContext} context - Provides details about the document being saved.
|
|
19
|
+
* @returns {Promise<void>} Resolves when your operation is complete.
|
|
20
|
+
*/
|
|
21
|
+
onSetDoc: (context: OnSetDocContext) => Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
export type HookConfig = OnSetDocConfig;
|
|
24
|
+
export type HookFn = (config: SatelliteConfigEnv) => HookConfig;
|
|
25
|
+
export type HookFnOrObject = HookConfig | HookFn;
|
|
26
|
+
export declare function defineHook(config: HookConfig): HookConfig;
|
|
27
|
+
export declare function defineHook(config: HookFn): HookFn;
|
|
28
|
+
export declare function defineHook(config: HookFnOrObject): HookFnOrObject;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Placeholder for future environment-specific configurations.
|
|
3
|
+
*
|
|
4
|
+
* Currently unused, but it may support features such as:
|
|
5
|
+
* - Defining the execution mode (e.g., staging or production).
|
|
6
|
+
* - Providing environment-specific values like `ckBtcLedgerId` for test or production.
|
|
7
|
+
*/
|
|
8
|
+
export type SatelliteConfigEnv = object;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { RawUserId } from './core';
|
|
2
|
+
import type { DocAssertSet, DocUpsert } from './datastore';
|
|
3
|
+
/**
|
|
4
|
+
* Represents the context provided to hooks, containing information about the caller and related data.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The type of data associated with the hook.
|
|
7
|
+
*/
|
|
8
|
+
export interface HookContext<T> {
|
|
9
|
+
/**
|
|
10
|
+
* The user who originally triggered the function that in turn triggered the hook.
|
|
11
|
+
*/
|
|
12
|
+
caller: RawUserId;
|
|
13
|
+
/**
|
|
14
|
+
* The data associated with the hook execution.
|
|
15
|
+
*/
|
|
16
|
+
data: T;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Represents the context of a document operation within a collection.
|
|
20
|
+
*
|
|
21
|
+
* @template T - The type of data associated with the document.
|
|
22
|
+
*/
|
|
23
|
+
export interface DocContext<T> {
|
|
24
|
+
/**
|
|
25
|
+
* The name of the collection where the document is stored.
|
|
26
|
+
*/
|
|
27
|
+
collection: string;
|
|
28
|
+
/**
|
|
29
|
+
* The unique key identifying the document within the collection.
|
|
30
|
+
*/
|
|
31
|
+
key: string;
|
|
32
|
+
/**
|
|
33
|
+
* The data associated with the document operation.
|
|
34
|
+
*/
|
|
35
|
+
data: T;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The context provided to the `onSetDoc` hook.
|
|
39
|
+
*
|
|
40
|
+
* This context contains information about the document being created or updated,
|
|
41
|
+
* along with details about the user who triggered the operation.
|
|
42
|
+
*/
|
|
43
|
+
export type OnSetDocContext = HookContext<DocContext<DocUpsert>>;
|
|
44
|
+
/**
|
|
45
|
+
* The context provided to the `assertSetDoc` hook.
|
|
46
|
+
*
|
|
47
|
+
* This context contains information about the document being validated before
|
|
48
|
+
* it is created or updated. If validation fails, the developer should throw an error.
|
|
49
|
+
*/
|
|
50
|
+
export type AssertSetDocContext = HookContext<DocContext<DocAssertSet>>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a timestamp in nanoseconds since the Unix epoch.
|
|
3
|
+
*
|
|
4
|
+
* Used for tracking when events occur, such as document creation and updates.
|
|
5
|
+
*/
|
|
6
|
+
export type Timestamp = bigint;
|
|
7
|
+
/**
|
|
8
|
+
* Represents a version number for tracking changes.
|
|
9
|
+
*
|
|
10
|
+
* This is typically incremented with each update to ensure consistency.
|
|
11
|
+
*/
|
|
12
|
+
export type Version = bigint;
|
|
13
|
+
/**
|
|
14
|
+
* Represents raw binary data.
|
|
15
|
+
*
|
|
16
|
+
* This is used to store unstructured data in a document.
|
|
17
|
+
*/
|
|
18
|
+
export type RawData = Uint8Array;
|
|
19
|
+
/**
|
|
20
|
+
* Represents a raw principal identifier.
|
|
21
|
+
*
|
|
22
|
+
* Principals are unique identities used in authentication and authorization.
|
|
23
|
+
*/
|
|
24
|
+
export type RawPrincipal = Uint8Array;
|
|
25
|
+
/**
|
|
26
|
+
* Represents a raw user identifier.
|
|
27
|
+
*
|
|
28
|
+
* This is a principal associated with a user.
|
|
29
|
+
*/
|
|
30
|
+
export type RawUserId = RawPrincipal;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { RawData, RawUserId, Timestamp, Version } from './core';
|
|
2
|
+
/**
|
|
3
|
+
* Represents a document update operation.
|
|
4
|
+
*
|
|
5
|
+
* This is used in hooks where a document is either being created or updated.
|
|
6
|
+
*/
|
|
7
|
+
export interface DocUpsert {
|
|
8
|
+
/**
|
|
9
|
+
* The previous version of the document before the update.
|
|
10
|
+
* Undefined if this is a new document.
|
|
11
|
+
*/
|
|
12
|
+
before?: Doc;
|
|
13
|
+
/**
|
|
14
|
+
* The new version of the document after the update.
|
|
15
|
+
*/
|
|
16
|
+
after: Doc;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Represents a validation check before setting a document.
|
|
20
|
+
*
|
|
21
|
+
* The developer can compare the `current` and `proposed` versions and
|
|
22
|
+
* throw an error if their validation fails.
|
|
23
|
+
*/
|
|
24
|
+
export interface DocAssertSet {
|
|
25
|
+
/**
|
|
26
|
+
* The current version of the document before the operation.
|
|
27
|
+
* Undefined if this is a new document.
|
|
28
|
+
*/
|
|
29
|
+
current?: Doc;
|
|
30
|
+
/**
|
|
31
|
+
* The proposed version of the document.
|
|
32
|
+
* This can be validated before allowing the operation.
|
|
33
|
+
*/
|
|
34
|
+
proposed: SetDoc;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Represents a document stored in a collection.
|
|
38
|
+
*/
|
|
39
|
+
export interface Doc {
|
|
40
|
+
/**
|
|
41
|
+
* The user who owns this document.
|
|
42
|
+
*/
|
|
43
|
+
owner: RawUserId;
|
|
44
|
+
/**
|
|
45
|
+
* The raw data of the document.
|
|
46
|
+
*/
|
|
47
|
+
data: RawData;
|
|
48
|
+
/**
|
|
49
|
+
* An optional description of the document.
|
|
50
|
+
*/
|
|
51
|
+
description?: string;
|
|
52
|
+
/**
|
|
53
|
+
* The timestamp when the document was first created.
|
|
54
|
+
*/
|
|
55
|
+
created_at: Timestamp;
|
|
56
|
+
/**
|
|
57
|
+
* The timestamp when the document was last updated.
|
|
58
|
+
*/
|
|
59
|
+
updated_at: Timestamp;
|
|
60
|
+
/**
|
|
61
|
+
* The version number of the document, used for consistency checks.
|
|
62
|
+
* If not provided, it's assumed to be the first version.
|
|
63
|
+
*/
|
|
64
|
+
version?: Version;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Represents a request to set or update a document.
|
|
68
|
+
*
|
|
69
|
+
* This is used when submitting new document data.
|
|
70
|
+
*/
|
|
71
|
+
export interface SetDoc {
|
|
72
|
+
/**
|
|
73
|
+
* The raw data of the document.
|
|
74
|
+
*/
|
|
75
|
+
data: RawData;
|
|
76
|
+
/**
|
|
77
|
+
* An optional description of the document.
|
|
78
|
+
*/
|
|
79
|
+
description?: string;
|
|
80
|
+
/**
|
|
81
|
+
* The expected version number to ensure consistency.
|
|
82
|
+
* If provided, the operation will fail if the stored version does not match.
|
|
83
|
+
*/
|
|
84
|
+
version?: Version;
|
|
85
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type * from './configs/assert.config';
|
|
2
|
+
export type * from './configs/collection.config';
|
|
3
|
+
export type * from './configs/hook.config';
|
|
4
|
+
export type * from './configs/satellite.config';
|
|
5
|
+
export type * from './hooks/context';
|
|
6
|
+
export type * from './hooks/core';
|
|
7
|
+
export type * from './hooks/datastore';
|
|
8
|
+
export * from './sdk/datastore.sdk';
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@junobuild/functions",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "JavaScript and TypeScript utilities for Juno Serverless Functions",
|
|
5
|
+
"author": "David Dal Busco (https://daviddalbusco.com)",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/browser/index.js",
|
|
9
|
+
"module": "./dist/browser/index.js",
|
|
10
|
+
"types": "./dist/types/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/types/index.d.ts",
|
|
15
|
+
"default": "./dist/browser/index.js"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"rmdir": "node ../../scripts/rmdir.mjs",
|
|
26
|
+
"ts-declaration": "tsc --emitDeclarationOnly --outDir dist/types && cp -R src/*.d.ts dist/types/",
|
|
27
|
+
"build": "npm run rmdir && mkdir -p dist && node esbuild.mjs && npm run ts-declaration",
|
|
28
|
+
"prepack": "npm run build"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/junobuild/juno-js.git",
|
|
33
|
+
"directory": "packages/functions"
|
|
34
|
+
},
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/junobuild/juno-js"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://juno.build",
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@dfinity/utils": "^2"
|
|
41
|
+
}
|
|
42
|
+
}
|