@dfinity/utils 0.0.1 → 0.0.2-next-2022-09-22

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 CHANGED
@@ -1,3 +1,87 @@
1
1
  # utils-js
2
2
 
3
3
  A collection of utilities and constants for NNS/SNS projects.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@dfinity/utils.svg?logo=npm)](https://www.npmjs.com/package/@dfinity/utils) [![GitHub license](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
6
+
7
+ ## Table of contents
8
+
9
+ - [Installation](#installation)
10
+ - [Features](#features)
11
+
12
+ ## Installation
13
+
14
+ You can use `utils-js` by installing it in your project.
15
+
16
+ ```bash
17
+ npm i @dfinity/utils
18
+ ```
19
+
20
+ The bundle needs peer dependencies, be sure that following resources are available in your project as well.
21
+
22
+ ```bash
23
+ npm i @dfinity/agent @dfinity/candid @dfinity/principal
24
+ ```
25
+
26
+ ## Features
27
+
28
+ `utils-js` implements following features:
29
+
30
+ <!-- TSDOC_START -->
31
+
32
+ ### :toolbox: Functions
33
+
34
+ - [defaultAgent](#gear-defaultagent)
35
+ - [createServices](#gear-createservices)
36
+ - [assertNonNullish](#gear-assertnonnullish)
37
+ - [toNullable](#gear-tonullable)
38
+ - [fromNullable](#gear-fromnullable)
39
+ - [fromDefinedNullable](#gear-fromdefinednullable)
40
+
41
+ #### :gear: defaultAgent
42
+
43
+ | Function | Type |
44
+ | -------------- | ------------- |
45
+ | `defaultAgent` | `() => Agent` |
46
+
47
+ #### :gear: createServices
48
+
49
+ | Function | Type |
50
+ | ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
51
+ | `createServices` | `<T>({ options: { canisterId, serviceOverride, certifiedServiceOverride, agent: agentOption, }, idlFactory, certifiedIdlFactory, }: { options: RequiredCanisterOptions<T>; idlFactory: InterfaceFactory; certifiedIdlFactory: InterfaceFactory; }) => { ...; }` |
52
+
53
+ #### :gear: assertNonNullish
54
+
55
+ | Function | Type |
56
+ | ------------------ | -------------------------------------------------------------------- |
57
+ | `assertNonNullish` | `<T>(value: T, message?: string) => asserts value is NonNullable<T>` |
58
+
59
+ #### :gear: toNullable
60
+
61
+ | Function | Type |
62
+ | ------------ | ----------------------------- |
63
+ | `toNullable` | `<T>(value?: T) => [] or [T]` |
64
+
65
+ #### :gear: fromNullable
66
+
67
+ | Function | Type |
68
+ | -------------- | ---------------------------- |
69
+ | `fromNullable` | `<T>(value: [] or [T]) => T` |
70
+
71
+ #### :gear: fromDefinedNullable
72
+
73
+ | Function | Type |
74
+ | --------------------- | ---------------------------- |
75
+ | `fromDefinedNullable` | `<T>(value: [] or [T]) => T` |
76
+
77
+ ### :factory: NullishError
78
+
79
+ #### Constructors
80
+
81
+ `public`
82
+
83
+ Parameters:
84
+
85
+ - `message`
86
+
87
+ <!-- TSDOC_END -->
@@ -1,2 +1,2 @@
1
- "use strict";var i=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var m=(t,e)=>{for(var o in e)i(t,o,{get:e[o],enumerable:!0})},l=(t,e,o,p)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of T(e))!d.call(t,n)&&n!==o&&i(t,n,{get:()=>e[n],enumerable:!(p=f(e,n))||p.enumerable});return t};var x=t=>l(i({},"__esModule",{value:!0}),t);var y={};m(y,{NullishError:()=>s,assertNonNullish:()=>a,defaultAgent:()=>g,fromNullable:()=>u,toNullable:()=>c});module.exports=x(y);var r=require("@dfinity/agent"),g=()=>new r.HttpAgent({host:"https://ic0.app",identity:new r.AnonymousIdentity});var s=class extends Error{},a=(t,e)=>{if(t==null)throw new s(e)};var c=t=>t?[t]:[],u=t=>t==null?void 0:t[0];0&&(module.exports={NullishError,assertNonNullish,defaultAgent,fromNullable,toNullable});
1
+ "use strict";var p=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var x=(t,e)=>{for(var o in e)p(t,o,{get:e[o],enumerable:!0})},b=(t,e,o,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of A(e))!g.call(t,r)&&r!==o&&p(t,r,{get:()=>e[r],enumerable:!(n=y(e,r))||n.enumerable});return t};var I=t=>b(p({},"__esModule",{value:!0}),t);var C={};x(C,{NullishError:()=>s,assertNonNullish:()=>m,createServices:()=>N,defaultAgent:()=>a,fromDefinedNullable:()=>O,fromNullable:()=>d,toNullable:()=>S});module.exports=I(C);var f=require("@dfinity/agent");var i=require("@dfinity/agent"),a=()=>new i.HttpAgent({host:"https://ic0.app",identity:new i.AnonymousIdentity});var N=({options:{canisterId:t,serviceOverride:e,certifiedServiceOverride:o,agent:n},idlFactory:r,certifiedIdlFactory:l})=>{let c=n??a(),T=e??f.Actor.createActor(r,{agent:c,canisterId:t}),u=o??f.Actor.createActor(l,{agent:c,canisterId:t});return{service:T,certifiedService:u,agent:c,canisterId:t}};var s=class extends Error{},m=(t,e)=>{if(t==null)throw new s(e)};var S=t=>t?[t]:[],d=t=>t==null?void 0:t[0],O=t=>{let e=d(t);return m(e),e};0&&(module.exports={NullishError,assertNonNullish,createServices,defaultAgent,fromDefinedNullable,fromNullable,toNullable});
2
2
  //# sourceMappingURL=index.cjs.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../../src/index.ts", "../../src/utils/agent.utils.ts", "../../src/utils/asserts.utils.ts", "../../src/utils/did.utils.ts"],
4
- "sourcesContent": ["export * from \"./utils/agent.utils\";\nexport * from \"./utils/asserts.utils\";\nexport * from \"./utils/did.utils\";\n", "import type { Agent } from \"@dfinity/agent\";\nimport { AnonymousIdentity, HttpAgent } from \"@dfinity/agent\";\n\n/**\n * @returns The default agent to use. An agent that connects to mainnet with the anonymous identity.\n */\nexport const defaultAgent = (): Agent =>\n new HttpAgent({\n host: \"https://ic0.app\",\n identity: new AnonymousIdentity(),\n });\n", "export class NullishError extends Error {}\n\nexport const assertNonNullish: <T>(\n value: T,\n message?: string\n) => asserts value is NonNullable<T> = <T>(\n value: T,\n message?: string\n): void => {\n if (value === null || value === undefined) {\n throw new NullishError(message);\n }\n};\n", "export const toNullable = <T>(value?: T): [] | [T] => {\n return value ? [value] : [];\n};\n\nexport const fromNullable = <T>(value: [] | [T]): T | undefined => {\n return value?.[0];\n};\n"],
5
- "mappings": "yaAAA,qICCA,MAA6C,0BAKhC,EAAe,IAC1B,GAAI,aAAU,CACZ,KAAM,kBACN,SAAU,GAAI,oBAChB,CAAC,ECVI,GAAM,GAAN,aAA2B,MAAM,CAAC,EAE5B,EAG0B,CACrC,EACA,IACS,CACT,GAAI,GAAU,KACZ,KAAM,IAAI,GAAa,CAAO,CAElC,ECZO,GAAM,GAAa,AAAI,GACrB,EAAQ,CAAC,CAAK,EAAI,CAAC,EAGf,EAAe,AAAI,GACvB,iBAAQ",
3
+ "sources": ["../../src/index.ts", "../../src/utils/actor.utils.ts", "../../src/utils/agent.utils.ts", "../../src/utils/asserts.utils.ts", "../../src/utils/did.utils.ts"],
4
+ "sourcesContent": ["export type { CanisterOptions } from \"./types/canister.options\";\nexport * from \"./utils/actor.utils\";\nexport * from \"./utils/agent.utils\";\nexport * from \"./utils/asserts.utils\";\nexport * from \"./utils/did.utils\";\n", "import type { ActorSubclass, Agent } from \"@dfinity/agent\";\nimport { Actor } from \"@dfinity/agent\";\nimport type { IDL } from \"@dfinity/candid\";\nimport type { Principal } from \"@dfinity/principal\";\nimport type { CanisterOptions } from \"../types/canister.options\";\nimport { defaultAgent } from \"./agent.utils\";\n\ntype RequiredCanisterOptions<T> = Required<\n Pick<CanisterOptions<T>, \"canisterId\">\n> &\n Omit<CanisterOptions<T>, \"canisterId\">;\n\nexport const createServices = <T>({\n options: {\n canisterId,\n serviceOverride,\n certifiedServiceOverride,\n agent: agentOption,\n },\n idlFactory,\n certifiedIdlFactory,\n}: {\n options: RequiredCanisterOptions<T>;\n idlFactory: IDL.InterfaceFactory;\n certifiedIdlFactory: IDL.InterfaceFactory;\n}): {\n service: ActorSubclass<T>;\n certifiedService: ActorSubclass<T>;\n agent: Agent;\n canisterId: Principal;\n} => {\n const agent: Agent = agentOption ?? defaultAgent();\n\n const service: ActorSubclass<T> =\n serviceOverride ??\n Actor.createActor<T>(idlFactory, {\n agent,\n canisterId,\n });\n\n const certifiedService: ActorSubclass<T> =\n certifiedServiceOverride ??\n Actor.createActor<T>(certifiedIdlFactory, {\n agent,\n canisterId,\n });\n\n return { service, certifiedService, agent, canisterId };\n};\n", "import type { Agent } from \"@dfinity/agent\";\nimport { AnonymousIdentity, HttpAgent } from \"@dfinity/agent\";\n\n/**\n * @returns The default agent to use. An agent that connects to mainnet with the anonymous identity.\n */\nexport const defaultAgent = (): Agent =>\n new HttpAgent({\n host: \"https://ic0.app\",\n identity: new AnonymousIdentity(),\n });\n", "export class NullishError extends Error {}\n\nexport const assertNonNullish: <T>(\n value: T,\n message?: string\n) => asserts value is NonNullable<T> = <T>(\n value: T,\n message?: string\n): void => {\n if (value === null || value === undefined) {\n throw new NullishError(message);\n }\n};\n", "import { assertNonNullish } from \"./asserts.utils\";\n\nexport const toNullable = <T>(value?: T): [] | [T] => {\n return value ? [value] : [];\n};\n\nexport const fromNullable = <T>(value: [] | [T]): T | undefined => {\n return value?.[0];\n};\n\nexport const fromDefinedNullable = <T>(value: [] | [T]): T => {\n const result = fromNullable(value);\n\n assertNonNullish(result);\n\n return result;\n};\n"],
5
+ "mappings": "yaAAA,oLCCA,MAAsB,0BCAtB,MAA6C,0BAKhC,EAAe,IAC1B,GAAI,aAAU,CACZ,KAAM,kBACN,SAAU,GAAI,oBAChB,CAAC,EDEI,GAAM,GAAiB,CAAI,CAChC,QAAS,CACP,aACA,kBACA,2BACA,MAAO,GAET,aACA,yBAUG,CACH,GAAM,GAAe,GAAe,EAAa,EAE3C,EACJ,GACA,QAAM,YAAe,EAAY,CAC/B,QACA,YACF,CAAC,EAEG,EACJ,GACA,QAAM,YAAe,EAAqB,CACxC,QACA,YACF,CAAC,EAEH,MAAO,CAAE,UAAS,mBAAkB,QAAO,YAAW,CACxD,EEhDO,GAAM,GAAN,aAA2B,MAAM,CAAC,EAE5B,EAG0B,CACrC,EACA,IACS,CACT,GAAI,GAAU,KACZ,KAAM,IAAI,GAAa,CAAO,CAElC,ECVO,GAAM,GAAa,AAAI,GACrB,EAAQ,CAAC,CAAK,EAAI,CAAC,EAGf,EAAe,AAAI,GACvB,iBAAQ,GAGJ,EAAsB,AAAI,GAAuB,CAC5D,GAAM,GAAS,EAAa,CAAK,EAEjC,SAAiB,CAAM,EAEhB,CACT",
6
6
  "names": []
7
7
  }
package/dist/esm/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import{AnonymousIdentity as o,HttpAgent as r}from"@dfinity/agent";var i=()=>new r({host:"https://ic0.app",identity:new o});var e=class extends Error{},l=(t,n)=>{if(t==null)throw new e(n)};var T=t=>t?[t]:[],a=t=>t?.[0];export{e as NullishError,l as assertNonNullish,i as defaultAgent,a as fromNullable,T as toNullable};
1
+ import{Actor as i}from"@dfinity/agent";import{AnonymousIdentity as d,HttpAgent as T}from"@dfinity/agent";var n=()=>new T({host:"https://ic0.app",identity:new d});var b=({options:{canisterId:t,serviceOverride:e,certifiedServiceOverride:c,agent:p},idlFactory:a,certifiedIdlFactory:f})=>{let r=p??n(),l=e??i.createActor(a,{agent:r,canisterId:t}),m=c??i.createActor(f,{agent:r,canisterId:t});return{service:l,certifiedService:m,agent:r,canisterId:t}};var o=class extends Error{},s=(t,e)=>{if(t==null)throw new o(e)};var O=t=>t?[t]:[],u=t=>t?.[0],v=t=>{let e=u(t);return s(e),e};export{o as NullishError,s as assertNonNullish,b as createServices,n as defaultAgent,v as fromDefinedNullable,u as fromNullable,O as toNullable};
2
2
  //# sourceMappingURL=index.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../../src/utils/agent.utils.ts", "../../src/utils/asserts.utils.ts", "../../src/utils/did.utils.ts"],
4
- "sourcesContent": ["import type { Agent } from \"@dfinity/agent\";\nimport { AnonymousIdentity, HttpAgent } from \"@dfinity/agent\";\n\n/**\n * @returns The default agent to use. An agent that connects to mainnet with the anonymous identity.\n */\nexport const defaultAgent = (): Agent =>\n new HttpAgent({\n host: \"https://ic0.app\",\n identity: new AnonymousIdentity(),\n });\n", "export class NullishError extends Error {}\n\nexport const assertNonNullish: <T>(\n value: T,\n message?: string\n) => asserts value is NonNullable<T> = <T>(\n value: T,\n message?: string\n): void => {\n if (value === null || value === undefined) {\n throw new NullishError(message);\n }\n};\n", "export const toNullable = <T>(value?: T): [] | [T] => {\n return value ? [value] : [];\n};\n\nexport const fromNullable = <T>(value: [] | [T]): T | undefined => {\n return value?.[0];\n};\n"],
5
- "mappings": "AACA,kEAKO,GAAM,GAAe,IAC1B,GAAI,GAAU,CACZ,KAAM,kBACN,SAAU,GAAI,EAChB,CAAC,ECVI,GAAM,GAAN,aAA2B,MAAM,CAAC,EAE5B,EAG0B,CACrC,EACA,IACS,CACT,GAAI,GAAU,KACZ,KAAM,IAAI,GAAa,CAAO,CAElC,ECZO,GAAM,GAAa,AAAI,GACrB,EAAQ,CAAC,CAAK,EAAI,CAAC,EAGf,EAAe,AAAI,GACvB,IAAQ",
3
+ "sources": ["../../src/utils/actor.utils.ts", "../../src/utils/agent.utils.ts", "../../src/utils/asserts.utils.ts", "../../src/utils/did.utils.ts"],
4
+ "sourcesContent": ["import type { ActorSubclass, Agent } from \"@dfinity/agent\";\nimport { Actor } from \"@dfinity/agent\";\nimport type { IDL } from \"@dfinity/candid\";\nimport type { Principal } from \"@dfinity/principal\";\nimport type { CanisterOptions } from \"../types/canister.options\";\nimport { defaultAgent } from \"./agent.utils\";\n\ntype RequiredCanisterOptions<T> = Required<\n Pick<CanisterOptions<T>, \"canisterId\">\n> &\n Omit<CanisterOptions<T>, \"canisterId\">;\n\nexport const createServices = <T>({\n options: {\n canisterId,\n serviceOverride,\n certifiedServiceOverride,\n agent: agentOption,\n },\n idlFactory,\n certifiedIdlFactory,\n}: {\n options: RequiredCanisterOptions<T>;\n idlFactory: IDL.InterfaceFactory;\n certifiedIdlFactory: IDL.InterfaceFactory;\n}): {\n service: ActorSubclass<T>;\n certifiedService: ActorSubclass<T>;\n agent: Agent;\n canisterId: Principal;\n} => {\n const agent: Agent = agentOption ?? defaultAgent();\n\n const service: ActorSubclass<T> =\n serviceOverride ??\n Actor.createActor<T>(idlFactory, {\n agent,\n canisterId,\n });\n\n const certifiedService: ActorSubclass<T> =\n certifiedServiceOverride ??\n Actor.createActor<T>(certifiedIdlFactory, {\n agent,\n canisterId,\n });\n\n return { service, certifiedService, agent, canisterId };\n};\n", "import type { Agent } from \"@dfinity/agent\";\nimport { AnonymousIdentity, HttpAgent } from \"@dfinity/agent\";\n\n/**\n * @returns The default agent to use. An agent that connects to mainnet with the anonymous identity.\n */\nexport const defaultAgent = (): Agent =>\n new HttpAgent({\n host: \"https://ic0.app\",\n identity: new AnonymousIdentity(),\n });\n", "export class NullishError extends Error {}\n\nexport const assertNonNullish: <T>(\n value: T,\n message?: string\n) => asserts value is NonNullable<T> = <T>(\n value: T,\n message?: string\n): void => {\n if (value === null || value === undefined) {\n throw new NullishError(message);\n }\n};\n", "import { assertNonNullish } from \"./asserts.utils\";\n\nexport const toNullable = <T>(value?: T): [] | [T] => {\n return value ? [value] : [];\n};\n\nexport const fromNullable = <T>(value: [] | [T]): T | undefined => {\n return value?.[0];\n};\n\nexport const fromDefinedNullable = <T>(value: [] | [T]): T => {\n const result = fromNullable(value);\n\n assertNonNullish(result);\n\n return result;\n};\n"],
5
+ "mappings": "AACA,uCCAA,kEAKO,GAAM,GAAe,IAC1B,GAAI,GAAU,CACZ,KAAM,kBACN,SAAU,GAAI,EAChB,CAAC,EDEI,GAAM,GAAiB,CAAI,CAChC,QAAS,CACP,aACA,kBACA,2BACA,MAAO,GAET,aACA,yBAUG,CACH,GAAM,GAAe,GAAe,EAAa,EAE3C,EACJ,GACA,EAAM,YAAe,EAAY,CAC/B,QACA,YACF,CAAC,EAEG,EACJ,GACA,EAAM,YAAe,EAAqB,CACxC,QACA,YACF,CAAC,EAEH,MAAO,CAAE,UAAS,mBAAkB,QAAO,YAAW,CACxD,EEhDO,GAAM,GAAN,aAA2B,MAAM,CAAC,EAE5B,EAG0B,CACrC,EACA,IACS,CACT,GAAI,GAAU,KACZ,KAAM,IAAI,GAAa,CAAO,CAElC,ECVO,GAAM,GAAa,AAAI,GACrB,EAAQ,CAAC,CAAK,EAAI,CAAC,EAGf,EAAe,AAAI,GACvB,IAAQ,GAGJ,EAAsB,AAAI,GAAuB,CAC5D,GAAM,GAAS,EAAa,CAAK,EAEjC,SAAiB,CAAM,EAEhB,CACT",
6
6
  "names": []
7
7
  }
@@ -1,3 +1,5 @@
1
+ export type { CanisterOptions } from "./types/canister.options";
2
+ export * from "./utils/actor.utils";
1
3
  export * from "./utils/agent.utils";
2
4
  export * from "./utils/asserts.utils";
3
5
  export * from "./utils/did.utils";
@@ -0,0 +1,8 @@
1
+ import type { ActorSubclass, Agent } from "@dfinity/agent";
2
+ import type { Principal } from "@dfinity/principal";
3
+ export interface CanisterOptions<T> {
4
+ agent?: Agent;
5
+ canisterId?: Principal;
6
+ serviceOverride?: ActorSubclass<T>;
7
+ certifiedServiceOverride?: ActorSubclass<T>;
8
+ }
@@ -0,0 +1,16 @@
1
+ import type { ActorSubclass, Agent } from "@dfinity/agent";
2
+ import type { IDL } from "@dfinity/candid";
3
+ import type { Principal } from "@dfinity/principal";
4
+ import type { CanisterOptions } from "../types/canister.options";
5
+ declare type RequiredCanisterOptions<T> = Required<Pick<CanisterOptions<T>, "canisterId">> & Omit<CanisterOptions<T>, "canisterId">;
6
+ export declare const createServices: <T>({ options: { canisterId, serviceOverride, certifiedServiceOverride, agent: agentOption, }, idlFactory, certifiedIdlFactory, }: {
7
+ options: RequiredCanisterOptions<T>;
8
+ idlFactory: IDL.InterfaceFactory;
9
+ certifiedIdlFactory: IDL.InterfaceFactory;
10
+ }) => {
11
+ service: ActorSubclass<T>;
12
+ certifiedService: ActorSubclass<T>;
13
+ agent: Agent;
14
+ canisterId: Principal;
15
+ };
16
+ export {};
@@ -1,2 +1,3 @@
1
1
  export declare const toNullable: <T>(value?: T | undefined) => [] | [T];
2
2
  export declare const fromNullable: <T>(value: [] | [T]) => T | undefined;
3
+ export declare const fromDefinedNullable: <T>(value: [] | [T]) => T;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dfinity/utils",
3
- "version": "0.0.1",
3
+ "version": "0.0.2-next-2022-09-22",
4
4
  "description": "A collection of utilities and constants for NNS/SNS projects.",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/cjs/index.cjs.js",
@@ -48,4 +48,4 @@
48
48
  "service nervous system",
49
49
  "service-nervous-system"
50
50
  ]
51
- }
51
+ }