@strivacity/sdk-core 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.
Files changed (89) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/LICENSE +21 -0
  3. package/README.md +54 -0
  4. package/dist/Metadata.cjs +2 -0
  5. package/dist/Metadata.cjs.map +1 -0
  6. package/dist/Metadata.d.ts +74 -0
  7. package/dist/Metadata.mjs +2 -0
  8. package/dist/Metadata.mjs.map +1 -0
  9. package/dist/Session.cjs +2 -0
  10. package/dist/Session.cjs.map +1 -0
  11. package/dist/Session.d.ts +82 -0
  12. package/dist/Session.mjs +2 -0
  13. package/dist/Session.mjs.map +1 -0
  14. package/dist/State.cjs +2 -0
  15. package/dist/State.cjs.map +1 -0
  16. package/dist/State.d.ts +43 -0
  17. package/dist/State.mjs +2 -0
  18. package/dist/State.mjs.map +1 -0
  19. package/dist/flows/BaseFlow.cjs +2 -0
  20. package/dist/flows/BaseFlow.cjs.map +1 -0
  21. package/dist/flows/BaseFlow.d.ts +177 -0
  22. package/dist/flows/BaseFlow.mjs +2 -0
  23. package/dist/flows/BaseFlow.mjs.map +1 -0
  24. package/dist/flows/PopupFlow.cjs +2 -0
  25. package/dist/flows/PopupFlow.cjs.map +1 -0
  26. package/dist/flows/PopupFlow.d.ts +31 -0
  27. package/dist/flows/PopupFlow.mjs +2 -0
  28. package/dist/flows/PopupFlow.mjs.map +1 -0
  29. package/dist/flows/RedirectFlow.cjs +2 -0
  30. package/dist/flows/RedirectFlow.cjs.map +1 -0
  31. package/dist/flows/RedirectFlow.d.ts +32 -0
  32. package/dist/flows/RedirectFlow.mjs +2 -0
  33. package/dist/flows/RedirectFlow.mjs.map +1 -0
  34. package/dist/index.cjs +2 -0
  35. package/dist/index.cjs.map +1 -0
  36. package/dist/index.d.ts +19 -0
  37. package/dist/index.mjs +2 -0
  38. package/dist/index.mjs.map +1 -0
  39. package/dist/storages/LocalStorage.cjs +2 -0
  40. package/dist/storages/LocalStorage.cjs.map +1 -0
  41. package/dist/storages/LocalStorage.d.ts +23 -0
  42. package/dist/storages/LocalStorage.mjs +2 -0
  43. package/dist/storages/LocalStorage.mjs.map +1 -0
  44. package/dist/storages/SessionStorage.cjs +2 -0
  45. package/dist/storages/SessionStorage.cjs.map +1 -0
  46. package/dist/storages/SessionStorage.d.ts +23 -0
  47. package/dist/storages/SessionStorage.mjs +2 -0
  48. package/dist/storages/SessionStorage.mjs.map +1 -0
  49. package/dist/types.cjs +2 -0
  50. package/dist/types.cjs.map +1 -0
  51. package/dist/types.d.ts +861 -0
  52. package/dist/types.mjs +2 -0
  53. package/dist/types.mjs.map +1 -0
  54. package/dist/utils/base64Url.cjs +2 -0
  55. package/dist/utils/base64Url.cjs.map +1 -0
  56. package/dist/utils/base64Url.d.ts +27 -0
  57. package/dist/utils/base64Url.mjs +2 -0
  58. package/dist/utils/base64Url.mjs.map +1 -0
  59. package/dist/utils/constants.cjs +2 -0
  60. package/dist/utils/constants.cjs.map +1 -0
  61. package/dist/utils/constants.d.ts +6 -0
  62. package/dist/utils/constants.mjs +2 -0
  63. package/dist/utils/constants.mjs.map +1 -0
  64. package/dist/utils/crypto.cjs +2 -0
  65. package/dist/utils/crypto.cjs.map +1 -0
  66. package/dist/utils/crypto.d.ts +32 -0
  67. package/dist/utils/crypto.mjs +2 -0
  68. package/dist/utils/crypto.mjs.map +1 -0
  69. package/dist/utils/date.cjs +2 -0
  70. package/dist/utils/date.cjs.map +1 -0
  71. package/dist/utils/date.d.ts +6 -0
  72. package/dist/utils/date.mjs +2 -0
  73. package/dist/utils/date.mjs.map +1 -0
  74. package/dist/utils/fetch.cjs +2 -0
  75. package/dist/utils/fetch.cjs.map +1 -0
  76. package/dist/utils/fetch.d.ts +2 -0
  77. package/dist/utils/fetch.mjs +2 -0
  78. package/dist/utils/fetch.mjs.map +1 -0
  79. package/dist/utils/handlers.cjs +2 -0
  80. package/dist/utils/handlers.cjs.map +1 -0
  81. package/dist/utils/handlers.d.ts +34 -0
  82. package/dist/utils/handlers.mjs +2 -0
  83. package/dist/utils/handlers.mjs.map +1 -0
  84. package/dist/utils/jwt.cjs +2 -0
  85. package/dist/utils/jwt.cjs.map +1 -0
  86. package/dist/utils/jwt.d.ts +34 -0
  87. package/dist/utils/jwt.mjs +2 -0
  88. package/dist/utils/jwt.mjs.map +1 -0
  89. package/package.json +49 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handlers.mjs","sources":["../../src/utils/handlers.ts"],"sourcesContent":["import type { RedirectParams, ResponseMode, PopupWindowParams, PopupWindowFeatures } from '../types';\n\nlet popupWindow: WindowProxy | null = null;\n\n/**\n * Handles URL redirection to a target window using a specified location method.\n *\n * @param {URL} url The URL to redirect to.\n * @param {RedirectParams} [options] Optional parameters for the redirection, including the target window and location method.\n * @param {boolean} [shouldWait=true] Whether to wait for the previous action (default is true).\n * @returns {Promise<void>} A promise that resolves when the redirection occurs.\n */\nasync function redirectUrlHandler(url: URL, options?: RedirectParams, shouldWait = true): Promise<void> {\n\tconst targetWindow = options?.targetWindow === 'top' ? window.top : window.self;\n\tconst method = options?.locationMethod || 'assign';\n\n\tif (targetWindow) {\n\t\ttargetWindow.location[method](url);\n\t}\n\n\tif (shouldWait) {\n\t\t// NOTE: Wait for the previous action\n\t\treturn new Promise(() => undefined);\n\t}\n}\n\n/**\n * Handles the callback after a redirect and parses the response from the URL.\n *\n * @param {string} [url=globalThis.window?.location.href] The URL to parse, defaults to the current window location.\n * @param {ResponseMode} responseMode The response mode, either 'query' or 'fragment'.\n * @returns {Record<string, string>} A key-value map of the parsed URL parameters.\n */\nfunction redirectCallbackHandler(url: string = globalThis.window?.location.href, responseMode: ResponseMode): Record<string, string> {\n\tconst data = new URL(url)[responseMode === 'query' ? 'search' : 'hash'].slice(1);\n\n\treturn Object.fromEntries(new URLSearchParams(data));\n}\n\n/**\n * Opens a popup window to handle URL redirection and resolves with the data received from the popup.\n *\n * @param {URL} url The URL to redirect to in the popup.\n * @param {PopupWindowParams} [options] Optional parameters for the popup, including window features and target.\n * @returns {Promise<Record<string, string>>} A promise that resolves to the data received from the popup window.\n * @throws {Error} If the popup window is blocked or closed by the user.\n */\nasync function popupUrlHandler(url: URL, options?: PopupWindowParams): Promise<Record<string, string>> {\n\tconst disposables = new Set<() => void>();\n\tconst closeWindow = (): void => {\n\t\tif (popupWindow) {\n\t\t\tif (!popupWindow.closed) {\n\t\t\t\tpopupWindow.close();\n\t\t\t}\n\n\t\t\tpopupWindow = null;\n\t\t}\n\n\t\tfor (const dispose of disposables) {\n\t\t\tdispose();\n\t\t}\n\n\t\tdisposables.clear();\n\t};\n\n\tif (popupWindow) {\n\t\tcloseWindow();\n\t}\n\n\tconst popupWindowFeatures: PopupWindowFeatures = {\n\t\tlocation: false,\n\t\ttoolbar: false,\n\t\theight: 640,\n\t\t...options?.popupWindowFeatures,\n\t};\n\n\tif (popupWindowFeatures.width === undefined) {\n\t\tpopupWindowFeatures.width = [800, 720, 600, 480].find((width) => width <= window.outerWidth / 1.618) ?? 360;\n\t}\n\n\tpopupWindowFeatures.left = Math.max(0, Math.round(window.screenX + (window.outerWidth - popupWindowFeatures.width) / 2));\n\n\tif (popupWindowFeatures.height !== undefined) {\n\t\tpopupWindowFeatures.top = Math.max(0, Math.round(window.screenY + (window.outerHeight - popupWindowFeatures.height) / 2));\n\t}\n\n\tpopupWindow = window.open(\n\t\tundefined,\n\t\toptions?.popupWindowTarget || '_blank',\n\t\tObject.entries(popupWindowFeatures)\n\t\t\t.filter(([, value]) => value !== null)\n\t\t\t.map(([key, value]) => `${key}=${typeof value !== 'boolean' ? (value as string) : value ? 'yes' : 'no'}`)\n\t\t\t.join(','),\n\t);\n\n\tif (!popupWindow) {\n\t\tthrow Error('Popup window blocked');\n\t}\n\n\tpopupWindow.focus();\n\tpopupWindow.location.replace(url);\n\n\tconst data = await new Promise<Record<string, string>>((resolve, reject) => {\n\t\tconst listener = async (event: MessageEvent<Record<string, string>>) => {\n\t\t\tif (event.origin === window.location.origin && event.source === popupWindow && event.data) {\n\t\t\t\tresolve(event.data);\n\t\t\t}\n\t\t};\n\n\t\tconst timer = setInterval(() => {\n\t\t\tif (popupWindow?.closed) {\n\t\t\t\tclearInterval(timer);\n\t\t\t\treject(Error('Popup closed by user'));\n\t\t\t}\n\t\t}, 500);\n\n\t\twindow.addEventListener('message', listener);\n\t\tdisposables.add(() => window.removeEventListener('message', listener));\n\t\tdisposables.add(() => clearInterval(timer));\n\t});\n\n\tcloseWindow();\n\n\treturn data;\n}\n\n/**\n * Handles the callback from a popup window, parsing the response parameters from the URL and posting them to the opener window.\n *\n * @param {ResponseMode} responseMode The response mode, either 'query' or 'fragment'.\n */\nfunction popupCallbackHandler(responseMode: ResponseMode): void {\n\tconst args: Record<string, string> = {};\n\n\tif (responseMode === 'fragment') {\n\t\tnew URLSearchParams(globalThis.window?.location.hash.replace('#', '?')).forEach((value, key) => (args[key] = value));\n\t} else {\n\t\tnew URLSearchParams(globalThis.window?.location.search).forEach((value, key) => (args[key] = value));\n\t}\n\n\tglobalThis.window?.opener?.postMessage(args, '*');\n}\n\nexport { redirectUrlHandler, redirectCallbackHandler, popupUrlHandler, popupCallbackHandler };\n"],"names":["popupWindow","redirectUrlHandler","url","options","shouldWait","targetWindow","method","redirectCallbackHandler","_a","responseMode","data","popupUrlHandler","disposables","closeWindow","dispose","popupWindowFeatures","width","value","key","resolve","reject","listener","event","timer","popupCallbackHandler","_b","_c","_d","args"],"mappings":"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"}
@@ -0,0 +1,2 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("./base64Url.cjs");function i(o){const e=o.split(".");if(e.length!==3)throw Error("Invalid JWT");return JSON.parse(n.Base64Url.decodeUnicode(e[1]))}async function l(o,e,t){const c=n.Base64Url.encode(new TextEncoder().encode(JSON.stringify(o))),d=n.Base64Url.encode(new TextEncoder().encode(JSON.stringify(e))),r=`${c}.${d}`,s=await window.crypto.subtle.sign({name:"ECDSA",hash:{name:"SHA-256"}},t,new TextEncoder().encode(r)),a=n.Base64Url.encode(new Uint8Array(s));return`${r}.${a}`}function u(o,e){const t=n.Base64Url.encode(new TextEncoder().encode(JSON.stringify(o))),c=n.Base64Url.encode(new TextEncoder().encode(JSON.stringify(e)));return`${`${t}.${c}`}.signature`}const g={decode:i,generate:l,generateUnsigned:u};exports.jwt=g;
2
+ //# sourceMappingURL=jwt.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwt.cjs","sources":["../../src/utils/jwt.ts"],"sourcesContent":["import type { JwtClaims } from '../types';\nimport { Base64Url } from './base64Url';\n\n/**\n * Decodes a JWT token and returns its claims.\n *\n * @template R The expected structure of the JWT claims.\n * @param {string} token The JWT token to decode.\n * @returns {R} The decoded JWT claims.\n * @throws {Error} If the token is invalid or has the wrong format.\n */\nfunction decode<R = JwtClaims>(token: string): R {\n\tconst sections = token.split('.');\n\n\tif (sections.length !== 3) {\n\t\tthrow Error('Invalid JWT');\n\t}\n\n\treturn JSON.parse(Base64Url.decodeUnicode(sections[1]));\n}\n\n/**\n * Generates a signed JWT token using the provided header, payload, and private key.\n *\n * @param {object} header The JWT header.\n * @param {object} payload The JWT payload.\n * @param {CryptoKey} privateKey The private key to sign the JWT.\n * @returns {Promise<string>} A promise that resolves to the signed JWT token.\n */\nasync function generate(header: object, payload: object, privateKey: CryptoKey): Promise<string> {\n\tconst encodedHeader = Base64Url.encode(new TextEncoder().encode(JSON.stringify(header)));\n\tconst encodedPayload = Base64Url.encode(new TextEncoder().encode(JSON.stringify(payload)));\n\tconst encodedToken = `${encodedHeader}.${encodedPayload}`;\n\n\tconst signature = await window.crypto.subtle.sign(\n\t\t{\n\t\t\tname: 'ECDSA',\n\t\t\thash: { name: 'SHA-256' },\n\t\t},\n\t\tprivateKey,\n\t\tnew TextEncoder().encode(encodedToken),\n\t);\n\n\tconst encodedSignature = Base64Url.encode(new Uint8Array(signature));\n\treturn `${encodedToken}.${encodedSignature}`;\n}\n\n/**\n * Generates an unsigned JWT token using the provided header and payload.\n * The token will not include a valid signature.\n *\n * @param {object} header The JWT header.\n * @param {object} payload The JWT payload.\n * @returns {string} The unsigned JWT token.\n */\nfunction generateUnsigned(header: object, payload: object): string {\n\tconst encodedHeader = Base64Url.encode(new TextEncoder().encode(JSON.stringify(header)));\n\tconst encodedPayload = Base64Url.encode(new TextEncoder().encode(JSON.stringify(payload)));\n\tconst encodedToken = `${encodedHeader}.${encodedPayload}`;\n\n\treturn `${encodedToken}.signature`;\n}\n\nexport const jwt = {\n\tdecode,\n\tgenerate,\n\tgenerateUnsigned,\n};\n"],"names":["decode","token","sections","Base64Url","generate","header","payload","privateKey","encodedHeader","encodedPayload","encodedToken","signature","encodedSignature","generateUnsigned","jwt"],"mappings":"mHAWA,SAASA,EAAsBC,EAAkB,CAC1C,MAAAC,EAAWD,EAAM,MAAM,GAAG,EAE5B,GAAAC,EAAS,SAAW,EACvB,MAAM,MAAM,aAAa,EAG1B,OAAO,KAAK,MAAMC,EAAA,UAAU,cAAcD,EAAS,CAAC,CAAC,CAAC,CACvD,CAUA,eAAeE,EAASC,EAAgBC,EAAiBC,EAAwC,CAC1F,MAAAC,EAAgBL,EAAAA,UAAU,OAAO,IAAI,YAAA,EAAc,OAAO,KAAK,UAAUE,CAAM,CAAC,CAAC,EACjFI,EAAiBN,EAAAA,UAAU,OAAO,IAAI,YAAA,EAAc,OAAO,KAAK,UAAUG,CAAO,CAAC,CAAC,EACnFI,EAAe,GAAGF,CAAa,IAAIC,CAAc,GAEjDE,EAAY,MAAM,OAAO,OAAO,OAAO,KAC5C,CACC,KAAM,QACN,KAAM,CAAE,KAAM,SAAU,CACzB,EACAJ,EACA,IAAI,YAAA,EAAc,OAAOG,CAAY,CAAA,EAGhCE,EAAmBT,EAAAA,UAAU,OAAO,IAAI,WAAWQ,CAAS,CAAC,EAC5D,MAAA,GAAGD,CAAY,IAAIE,CAAgB,EAC3C,CAUA,SAASC,EAAiBR,EAAgBC,EAAyB,CAC5D,MAAAE,EAAgBL,EAAAA,UAAU,OAAO,IAAI,YAAA,EAAc,OAAO,KAAK,UAAUE,CAAM,CAAC,CAAC,EACjFI,EAAiBN,EAAAA,UAAU,OAAO,IAAI,YAAA,EAAc,OAAO,KAAK,UAAUG,CAAO,CAAC,CAAC,EAGzF,MAAO,GAFc,GAAGE,CAAa,IAAIC,CAAc,EAEjC,YACvB,CAEO,MAAMK,EAAM,CAClB,OAAAd,EACA,SAAAI,EACA,iBAAAS,CACD"}
@@ -0,0 +1,34 @@
1
+ import { JwtClaims } from '../types';
2
+ /**
3
+ * Decodes a JWT token and returns its claims.
4
+ *
5
+ * @template R The expected structure of the JWT claims.
6
+ * @param {string} token The JWT token to decode.
7
+ * @returns {R} The decoded JWT claims.
8
+ * @throws {Error} If the token is invalid or has the wrong format.
9
+ */
10
+ declare function decode<R = JwtClaims>(token: string): R;
11
+ /**
12
+ * Generates a signed JWT token using the provided header, payload, and private key.
13
+ *
14
+ * @param {object} header The JWT header.
15
+ * @param {object} payload The JWT payload.
16
+ * @param {CryptoKey} privateKey The private key to sign the JWT.
17
+ * @returns {Promise<string>} A promise that resolves to the signed JWT token.
18
+ */
19
+ declare function generate(header: object, payload: object, privateKey: CryptoKey): Promise<string>;
20
+ /**
21
+ * Generates an unsigned JWT token using the provided header and payload.
22
+ * The token will not include a valid signature.
23
+ *
24
+ * @param {object} header The JWT header.
25
+ * @param {object} payload The JWT payload.
26
+ * @returns {string} The unsigned JWT token.
27
+ */
28
+ declare function generateUnsigned(header: object, payload: object): string;
29
+ export declare const jwt: {
30
+ decode: typeof decode;
31
+ generate: typeof generate;
32
+ generateUnsigned: typeof generateUnsigned;
33
+ };
34
+ export {};
@@ -0,0 +1,2 @@
1
+ import{Base64Url as n}from"./base64Url.mjs";function a(o){const e=o.split(".");if(e.length!==3)throw Error("Invalid JWT");return JSON.parse(n.decodeUnicode(e[1]))}async function g(o,e,d){const c=n.encode(new TextEncoder().encode(JSON.stringify(o))),t=n.encode(new TextEncoder().encode(JSON.stringify(e))),r=`${c}.${t}`,s=await window.crypto.subtle.sign({name:"ECDSA",hash:{name:"SHA-256"}},d,new TextEncoder().encode(r)),i=n.encode(new Uint8Array(s));return`${r}.${i}`}function w(o,e){const d=n.encode(new TextEncoder().encode(JSON.stringify(o))),c=n.encode(new TextEncoder().encode(JSON.stringify(e)));return`${`${d}.${c}`}.signature`}const f={decode:a,generate:g,generateUnsigned:w};export{f as jwt};
2
+ //# sourceMappingURL=jwt.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwt.mjs","sources":["../../src/utils/jwt.ts"],"sourcesContent":["import type { JwtClaims } from '../types';\nimport { Base64Url } from './base64Url';\n\n/**\n * Decodes a JWT token and returns its claims.\n *\n * @template R The expected structure of the JWT claims.\n * @param {string} token The JWT token to decode.\n * @returns {R} The decoded JWT claims.\n * @throws {Error} If the token is invalid or has the wrong format.\n */\nfunction decode<R = JwtClaims>(token: string): R {\n\tconst sections = token.split('.');\n\n\tif (sections.length !== 3) {\n\t\tthrow Error('Invalid JWT');\n\t}\n\n\treturn JSON.parse(Base64Url.decodeUnicode(sections[1]));\n}\n\n/**\n * Generates a signed JWT token using the provided header, payload, and private key.\n *\n * @param {object} header The JWT header.\n * @param {object} payload The JWT payload.\n * @param {CryptoKey} privateKey The private key to sign the JWT.\n * @returns {Promise<string>} A promise that resolves to the signed JWT token.\n */\nasync function generate(header: object, payload: object, privateKey: CryptoKey): Promise<string> {\n\tconst encodedHeader = Base64Url.encode(new TextEncoder().encode(JSON.stringify(header)));\n\tconst encodedPayload = Base64Url.encode(new TextEncoder().encode(JSON.stringify(payload)));\n\tconst encodedToken = `${encodedHeader}.${encodedPayload}`;\n\n\tconst signature = await window.crypto.subtle.sign(\n\t\t{\n\t\t\tname: 'ECDSA',\n\t\t\thash: { name: 'SHA-256' },\n\t\t},\n\t\tprivateKey,\n\t\tnew TextEncoder().encode(encodedToken),\n\t);\n\n\tconst encodedSignature = Base64Url.encode(new Uint8Array(signature));\n\treturn `${encodedToken}.${encodedSignature}`;\n}\n\n/**\n * Generates an unsigned JWT token using the provided header and payload.\n * The token will not include a valid signature.\n *\n * @param {object} header The JWT header.\n * @param {object} payload The JWT payload.\n * @returns {string} The unsigned JWT token.\n */\nfunction generateUnsigned(header: object, payload: object): string {\n\tconst encodedHeader = Base64Url.encode(new TextEncoder().encode(JSON.stringify(header)));\n\tconst encodedPayload = Base64Url.encode(new TextEncoder().encode(JSON.stringify(payload)));\n\tconst encodedToken = `${encodedHeader}.${encodedPayload}`;\n\n\treturn `${encodedToken}.signature`;\n}\n\nexport const jwt = {\n\tdecode,\n\tgenerate,\n\tgenerateUnsigned,\n};\n"],"names":["decode","token","sections","Base64Url","generate","header","payload","privateKey","encodedHeader","encodedPayload","encodedToken","signature","encodedSignature","generateUnsigned","jwt"],"mappings":"4CAWA,SAASA,EAAsBC,EAAkB,CAC1C,MAAAC,EAAWD,EAAM,MAAM,GAAG,EAE5B,GAAAC,EAAS,SAAW,EACvB,MAAM,MAAM,aAAa,EAG1B,OAAO,KAAK,MAAMC,EAAU,cAAcD,EAAS,CAAC,CAAC,CAAC,CACvD,CAUA,eAAeE,EAASC,EAAgBC,EAAiBC,EAAwC,CAC1F,MAAAC,EAAgBL,EAAU,OAAO,IAAI,YAAA,EAAc,OAAO,KAAK,UAAUE,CAAM,CAAC,CAAC,EACjFI,EAAiBN,EAAU,OAAO,IAAI,YAAA,EAAc,OAAO,KAAK,UAAUG,CAAO,CAAC,CAAC,EACnFI,EAAe,GAAGF,CAAa,IAAIC,CAAc,GAEjDE,EAAY,MAAM,OAAO,OAAO,OAAO,KAC5C,CACC,KAAM,QACN,KAAM,CAAE,KAAM,SAAU,CACzB,EACAJ,EACA,IAAI,YAAA,EAAc,OAAOG,CAAY,CAAA,EAGhCE,EAAmBT,EAAU,OAAO,IAAI,WAAWQ,CAAS,CAAC,EAC5D,MAAA,GAAGD,CAAY,IAAIE,CAAgB,EAC3C,CAUA,SAASC,EAAiBR,EAAgBC,EAAyB,CAC5D,MAAAE,EAAgBL,EAAU,OAAO,IAAI,YAAA,EAAc,OAAO,KAAK,UAAUE,CAAM,CAAC,CAAC,EACjFI,EAAiBN,EAAU,OAAO,IAAI,YAAA,EAAc,OAAO,KAAK,UAAUG,CAAO,CAAC,CAAC,EAGzF,MAAO,GAFc,GAAGE,CAAa,IAAIC,CAAc,EAEjC,YACvB,CAEO,MAAMK,EAAM,CAClB,OAAAd,EACA,SAAAI,EACA,iBAAAS,CACD"}
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@strivacity/sdk-core",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "description": "Strivacity JavaScript SDK client",
7
+ "author": "strivacity <info@strivacity.com>",
8
+ "main": "./dist/index.cjs",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.cjs",
15
+ "default": "./dist/index.mjs"
16
+ },
17
+ "./flows/*": {
18
+ "types": "./dist/flows/*.d.ts",
19
+ "import": "./dist/flows/*.mjs",
20
+ "require": "./dist/flows/*.cjs",
21
+ "default": "./dist/flows/*.mjs"
22
+ },
23
+ "./storages/*": {
24
+ "types": "./dist/storages/*.d.ts",
25
+ "import": "./dist/storages/*.mjs",
26
+ "require": "./dist/storages/*.cjs",
27
+ "default": "./dist/storages/*.mjs"
28
+ },
29
+ "./utils/*": {
30
+ "types": "./dist/utils/*.d.ts",
31
+ "import": "./dist/utils/*.mjs",
32
+ "require": "./dist/utils/*.cjs",
33
+ "default": "./dist/utils/*.mjs"
34
+ }
35
+ },
36
+ "typesVersions": {
37
+ "*": {
38
+ "flows/*": [
39
+ "./dist/flows/*"
40
+ ],
41
+ "storages/*": [
42
+ "./dist/storages/*"
43
+ ],
44
+ "utils/*": [
45
+ "./dist/utils/*"
46
+ ]
47
+ }
48
+ }
49
+ }