jmap-kit 0.0.0 → 1.0.2

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 (198) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +138 -3
  3. package/dist/src/capabilities/blob/blob.d.ts +83 -0
  4. package/dist/src/capabilities/blob/blob.js +98 -0
  5. package/dist/src/capabilities/blob/blob.js.map +1 -0
  6. package/dist/src/capabilities/blob/types.d.ts +212 -0
  7. package/dist/src/capabilities/blob/types.js +16 -0
  8. package/dist/src/capabilities/blob/types.js.map +1 -0
  9. package/dist/src/capabilities/blob-capability.d.ts +195 -0
  10. package/dist/src/capabilities/blob-capability.js +277 -0
  11. package/dist/src/capabilities/blob-capability.js.map +1 -0
  12. package/dist/src/capabilities/core/core.d.ts +47 -0
  13. package/dist/src/capabilities/core/core.js +59 -0
  14. package/dist/src/capabilities/core/core.js.map +1 -0
  15. package/dist/src/capabilities/core/types.d.ts +13 -0
  16. package/dist/src/capabilities/core/types.js +2 -0
  17. package/dist/src/capabilities/core/types.js.map +1 -0
  18. package/dist/src/capabilities/core-capability.d.ts +307 -0
  19. package/dist/src/capabilities/core-capability.js +344 -0
  20. package/dist/src/capabilities/core-capability.js.map +1 -0
  21. package/dist/src/capabilities/email/email.d.ts +124 -0
  22. package/dist/src/capabilities/email/email.js +136 -0
  23. package/dist/src/capabilities/email/email.js.map +1 -0
  24. package/dist/src/capabilities/email/types.d.ts +776 -0
  25. package/dist/src/capabilities/email/types.js +2 -0
  26. package/dist/src/capabilities/email/types.js.map +1 -0
  27. package/dist/src/capabilities/email-capability.d.ts +266 -0
  28. package/dist/src/capabilities/email-capability.js +241 -0
  29. package/dist/src/capabilities/email-capability.js.map +1 -0
  30. package/dist/src/capabilities/emailsubmission/emailsubmission.d.ts +95 -0
  31. package/dist/src/capabilities/emailsubmission/emailsubmission.js +107 -0
  32. package/dist/src/capabilities/emailsubmission/emailsubmission.js.map +1 -0
  33. package/dist/src/capabilities/emailsubmission/types.d.ts +256 -0
  34. package/dist/src/capabilities/emailsubmission/types.js +2 -0
  35. package/dist/src/capabilities/emailsubmission/types.js.map +1 -0
  36. package/dist/src/capabilities/example/example.d.ts +80 -0
  37. package/dist/src/capabilities/example/example.js +91 -0
  38. package/dist/src/capabilities/example/example.js.map +1 -0
  39. package/dist/src/capabilities/example/types.d.ts +33 -0
  40. package/dist/src/capabilities/example/types.js +2 -0
  41. package/dist/src/capabilities/example/types.js.map +1 -0
  42. package/dist/src/capabilities/identity/identity.d.ts +71 -0
  43. package/dist/src/capabilities/identity/identity.js +83 -0
  44. package/dist/src/capabilities/identity/identity.js.map +1 -0
  45. package/dist/src/capabilities/identity/types.d.ts +110 -0
  46. package/dist/src/capabilities/identity/types.js +2 -0
  47. package/dist/src/capabilities/identity/types.js.map +1 -0
  48. package/dist/src/capabilities/mailbox/mailbox.d.ts +91 -0
  49. package/dist/src/capabilities/mailbox/mailbox.js +103 -0
  50. package/dist/src/capabilities/mailbox/mailbox.js.map +1 -0
  51. package/dist/src/capabilities/mailbox/types.d.ts +248 -0
  52. package/dist/src/capabilities/mailbox/types.js +2 -0
  53. package/dist/src/capabilities/mailbox/types.js.map +1 -0
  54. package/dist/src/capabilities/maskedemail/maskedemail.d.ts +60 -0
  55. package/dist/src/capabilities/maskedemail/maskedemail.js +72 -0
  56. package/dist/src/capabilities/maskedemail/maskedemail.js.map +1 -0
  57. package/dist/src/capabilities/maskedemail/types.d.ts +67 -0
  58. package/dist/src/capabilities/maskedemail/types.js +4 -0
  59. package/dist/src/capabilities/maskedemail/types.js.map +1 -0
  60. package/dist/src/capabilities/maskedemail-capability.d.ts +112 -0
  61. package/dist/src/capabilities/maskedemail-capability.js +166 -0
  62. package/dist/src/capabilities/maskedemail-capability.js.map +1 -0
  63. package/dist/src/capabilities/searchsnippet/searchsnippet.d.ts +51 -0
  64. package/dist/src/capabilities/searchsnippet/searchsnippet.js +63 -0
  65. package/dist/src/capabilities/searchsnippet/searchsnippet.js.map +1 -0
  66. package/dist/src/capabilities/searchsnippet/types.d.ts +88 -0
  67. package/dist/src/capabilities/searchsnippet/types.js +2 -0
  68. package/dist/src/capabilities/searchsnippet/types.js.map +1 -0
  69. package/dist/src/capabilities/submission-capability.d.ts +89 -0
  70. package/dist/src/capabilities/submission-capability.js +75 -0
  71. package/dist/src/capabilities/submission-capability.js.map +1 -0
  72. package/dist/src/capabilities/thread/thread.d.ts +58 -0
  73. package/dist/src/capabilities/thread/thread.js +70 -0
  74. package/dist/src/capabilities/thread/thread.js.map +1 -0
  75. package/dist/src/capabilities/thread/types.d.ts +43 -0
  76. package/dist/src/capabilities/thread/types.js +2 -0
  77. package/dist/src/capabilities/thread/types.js.map +1 -0
  78. package/dist/src/capabilities/utils/assert-invocation-datatype.d.ts +7 -0
  79. package/dist/src/capabilities/utils/assert-invocation-datatype.js +13 -0
  80. package/dist/src/capabilities/utils/assert-invocation-datatype.js.map +1 -0
  81. package/dist/src/capabilities/utils/assert-invocation-method.d.ts +7 -0
  82. package/dist/src/capabilities/utils/assert-invocation-method.js +13 -0
  83. package/dist/src/capabilities/utils/assert-invocation-method.js.map +1 -0
  84. package/dist/src/capabilities/utils/assert-invocation.d.ts +7 -0
  85. package/dist/src/capabilities/utils/assert-invocation.js +22 -0
  86. package/dist/src/capabilities/utils/assert-invocation.js.map +1 -0
  87. package/dist/src/capabilities/utils/assert-non-nullish.d.ts +1 -0
  88. package/dist/src/capabilities/utils/assert-non-nullish.js +6 -0
  89. package/dist/src/capabilities/utils/assert-non-nullish.js.map +1 -0
  90. package/dist/src/capabilities/utils/create-readonly-account-validator.d.ts +49 -0
  91. package/dist/src/capabilities/utils/create-readonly-account-validator.js +80 -0
  92. package/dist/src/capabilities/utils/create-readonly-account-validator.js.map +1 -0
  93. package/dist/src/capabilities/vacationresponse/types.d.ts +100 -0
  94. package/dist/src/capabilities/vacationresponse/types.js +2 -0
  95. package/dist/src/capabilities/vacationresponse/types.js.map +1 -0
  96. package/dist/src/capabilities/vacationresponse/vacationresponse.d.ts +61 -0
  97. package/dist/src/capabilities/vacationresponse/vacationresponse.js +73 -0
  98. package/dist/src/capabilities/vacationresponse/vacationresponse.js.map +1 -0
  99. package/dist/src/capabilities/vacationresponse-capability.d.ts +65 -0
  100. package/dist/src/capabilities/vacationresponse-capability.js +68 -0
  101. package/dist/src/capabilities/vacationresponse-capability.js.map +1 -0
  102. package/dist/src/capability-registry/capability-registry.d.ts +148 -0
  103. package/dist/src/capability-registry/capability-registry.js +360 -0
  104. package/dist/src/capability-registry/capability-registry.js.map +1 -0
  105. package/dist/src/capability-registry/types.d.ts +385 -0
  106. package/dist/src/capability-registry/types.js +2 -0
  107. package/dist/src/capability-registry/types.js.map +1 -0
  108. package/dist/src/capability-registry/utils.d.ts +71 -0
  109. package/dist/src/capability-registry/utils.js +163 -0
  110. package/dist/src/capability-registry/utils.js.map +1 -0
  111. package/dist/src/common/registry.d.ts +366 -0
  112. package/dist/src/common/registry.js +321 -0
  113. package/dist/src/common/registry.js.map +1 -0
  114. package/dist/src/common/types.d.ts +338 -0
  115. package/dist/src/common/types.js +21 -0
  116. package/dist/src/common/types.js.map +1 -0
  117. package/dist/src/common/utils.d.ts +20 -0
  118. package/dist/src/common/utils.js +26 -0
  119. package/dist/src/common/utils.js.map +1 -0
  120. package/dist/src/index.d.ts +40 -0
  121. package/dist/src/index.js +33 -0
  122. package/dist/src/index.js.map +1 -0
  123. package/dist/src/invocation/arguments-proxy.d.ts +14 -0
  124. package/dist/src/invocation/arguments-proxy.js +37 -0
  125. package/dist/src/invocation/arguments-proxy.js.map +1 -0
  126. package/dist/src/invocation/error-invocation.d.ts +27 -0
  127. package/dist/src/invocation/error-invocation.js +39 -0
  128. package/dist/src/invocation/error-invocation.js.map +1 -0
  129. package/dist/src/invocation/invocation.d.ts +111 -0
  130. package/dist/src/invocation/invocation.js +158 -0
  131. package/dist/src/invocation/invocation.js.map +1 -0
  132. package/dist/src/invocation/result-reference.d.ts +86 -0
  133. package/dist/src/invocation/result-reference.js +118 -0
  134. package/dist/src/invocation/result-reference.js.map +1 -0
  135. package/dist/src/invocation/types.d.ts +637 -0
  136. package/dist/src/invocation/types.js +2 -0
  137. package/dist/src/invocation/types.js.map +1 -0
  138. package/dist/src/invocation/utils.d.ts +21 -0
  139. package/dist/src/invocation/utils.js +30 -0
  140. package/dist/src/invocation/utils.js.map +1 -0
  141. package/dist/src/invocation-factory/invocation-factory-manager.d.ts +20 -0
  142. package/dist/src/invocation-factory/invocation-factory-manager.js +50 -0
  143. package/dist/src/invocation-factory/invocation-factory-manager.js.map +1 -0
  144. package/dist/src/invocation-factory/invocation-list.d.ts +32 -0
  145. package/dist/src/invocation-factory/invocation-list.js +77 -0
  146. package/dist/src/invocation-factory/invocation-list.js.map +1 -0
  147. package/dist/src/invocation-factory/types.d.ts +11 -0
  148. package/dist/src/invocation-factory/types.js +2 -0
  149. package/dist/src/invocation-factory/types.js.map +1 -0
  150. package/dist/src/jmap-client/jmap-client.d.ts +252 -0
  151. package/dist/src/jmap-client/jmap-client.js +777 -0
  152. package/dist/src/jmap-client/jmap-client.js.map +1 -0
  153. package/dist/src/jmap-client/types.d.ts +427 -0
  154. package/dist/src/jmap-client/types.js +21 -0
  155. package/dist/src/jmap-client/types.js.map +1 -0
  156. package/dist/src/jmap-client/utils/abort-controller.d.ts +8 -0
  157. package/dist/src/jmap-client/utils/abort-controller.js +24 -0
  158. package/dist/src/jmap-client/utils/abort-controller.js.map +1 -0
  159. package/dist/src/jmap-client/utils/assert-connected.d.ts +7 -0
  160. package/dist/src/jmap-client/utils/assert-connected.js +11 -0
  161. package/dist/src/jmap-client/utils/assert-connected.js.map +1 -0
  162. package/dist/src/jmap-client/utils/deep-freeze.d.ts +7 -0
  163. package/dist/src/jmap-client/utils/deep-freeze.js +17 -0
  164. package/dist/src/jmap-client/utils/deep-freeze.js.map +1 -0
  165. package/dist/src/jmap-client/utils/emitter.d.ts +9 -0
  166. package/dist/src/jmap-client/utils/emitter.js +18 -0
  167. package/dist/src/jmap-client/utils/emitter.js.map +1 -0
  168. package/dist/src/jmap-client/utils/filter-session-capabilities.d.ts +22 -0
  169. package/dist/src/jmap-client/utils/filter-session-capabilities.js +40 -0
  170. package/dist/src/jmap-client/utils/filter-session-capabilities.js.map +1 -0
  171. package/dist/src/jmap-client/utils/jmap-request-error.d.ts +28 -0
  172. package/dist/src/jmap-client/utils/jmap-request-error.js +48 -0
  173. package/dist/src/jmap-client/utils/jmap-request-error.js.map +1 -0
  174. package/dist/src/jmap-client/utils/logger.d.ts +6 -0
  175. package/dist/src/jmap-client/utils/logger.js +22 -0
  176. package/dist/src/jmap-client/utils/logger.js.map +1 -0
  177. package/dist/src/jmap-client/utils/merge-headers.d.ts +11 -0
  178. package/dist/src/jmap-client/utils/merge-headers.js +40 -0
  179. package/dist/src/jmap-client/utils/merge-headers.js.map +1 -0
  180. package/dist/src/jmap-client/utils/template-utils.d.ts +27 -0
  181. package/dist/src/jmap-client/utils/template-utils.js +61 -0
  182. package/dist/src/jmap-client/utils/template-utils.js.map +1 -0
  183. package/dist/src/jmap-client/utils/track-utils.d.ts +19 -0
  184. package/dist/src/jmap-client/utils/track-utils.js +35 -0
  185. package/dist/src/jmap-client/utils/track-utils.js.map +1 -0
  186. package/dist/src/jmap-client/utils/transport.d.ts +12 -0
  187. package/dist/src/jmap-client/utils/transport.js +38 -0
  188. package/dist/src/jmap-client/utils/transport.js.map +1 -0
  189. package/dist/src/jmap-client/utils/validate-session.d.ts +19 -0
  190. package/dist/src/jmap-client/utils/validate-session.js +29 -0
  191. package/dist/src/jmap-client/utils/validate-session.js.map +1 -0
  192. package/dist/src/request-builder/request-builder.d.ts +95 -0
  193. package/dist/src/request-builder/request-builder.js +343 -0
  194. package/dist/src/request-builder/request-builder.js.map +1 -0
  195. package/dist/src/request-builder/types.d.ts +32 -0
  196. package/dist/src/request-builder/types.js +2 -0
  197. package/dist/src/request-builder/types.js.map +1 -0
  198. package/package.json +69 -3
@@ -0,0 +1,30 @@
1
+ import { ErrorInvocation } from "./error-invocation.js";
2
+ import { ResultReference } from "./result-reference.js";
3
+ /**
4
+ * Type guard to check if a value is an instance of ResultReference.
5
+ * @param value The value to check.
6
+ * @returns True if the value is a ResultReference, false otherwise.
7
+ */
8
+ export function isResultReference(value) {
9
+ return value instanceof ResultReference;
10
+ }
11
+ /**
12
+ * Type guard to check if an object is an ErrorInvocation instance.
13
+ * @param obj The object to check.
14
+ * @returns True if the object is an ErrorInvocation, false otherwise.
15
+ */
16
+ export function isErrorInvocation(obj) {
17
+ return obj instanceof ErrorInvocation;
18
+ }
19
+ /**
20
+ * Type guard to check if an object is a valid JMAPResponseInvocationErrorArgs.
21
+ * @param args The object to check.
22
+ * @returns True if the object has a string `type` property, false otherwise.
23
+ */
24
+ export function isJMAPResponseInvocationErrorArgs(args) {
25
+ return (typeof args === "object" &&
26
+ args !== null &&
27
+ "type" in args &&
28
+ typeof args.type === "string");
29
+ }
30
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/invocation/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAc;IAC5C,OAAO,KAAK,YAAY,eAAe,CAAC;AAC5C,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAC,GAAY;IAC1C,OAAO,GAAG,YAAY,eAAe,CAAC;AAC1C,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,iCAAiC,CAAC,IAAa;IAC3D,OAAO,CACH,OAAO,IAAI,KAAK,QAAQ;QACxB,IAAI,KAAK,IAAI;QACb,MAAM,IAAI,IAAI;QACd,OAAQ,IAA0B,CAAC,IAAI,KAAK,QAAQ,CACvD,CAAC;AACN,CAAC"}
@@ -0,0 +1,20 @@
1
+ import type { Id, JMAPResponseInvocation } from "../common/types.js";
2
+ import type { ClientContext, JMAPClientInterface } from "../jmap-client/types.js";
3
+ import { InvocationList } from "./invocation-list.js";
4
+ /**
5
+ * Manages construction of invocations from JMAP method responses using the capability registry.
6
+ */
7
+ export declare class InvocationFactoryManager {
8
+ #private;
9
+ constructor(client: JMAPClientInterface<unknown>, { logger, emitter }: ClientContext);
10
+ /**
11
+ * Construct invocation objects from JMAP method responses.
12
+ *
13
+ * @param methodResponses The array of JMAP method responses.
14
+ * @param reverseIdMap Map from method call IDs to internal symbols.
15
+ * @returns An InvocationList containing the constructed invocation objects.
16
+ * @throws If no factory is registered for a data type, or there's no factory
17
+ * function for the particular method.
18
+ */
19
+ createInvocations(methodResponses: JMAPResponseInvocation[], reverseIdMap: Map<Id, symbol>): InvocationList<import("../index.js").BaseInvocationArgs>;
20
+ }
@@ -0,0 +1,50 @@
1
+ import { ErrorInvocation } from "../invocation/error-invocation.js";
2
+ import { isJMAPResponseInvocationErrorArgs } from "../invocation/utils.js";
3
+ import { InvocationList } from "./invocation-list.js";
4
+ /**
5
+ * Manages construction of invocations from JMAP method responses using the capability registry.
6
+ */
7
+ export class InvocationFactoryManager {
8
+ #logger;
9
+ #emitter;
10
+ #client;
11
+ constructor(client, { logger, emitter }) {
12
+ this.#logger = logger;
13
+ this.#emitter = emitter;
14
+ this.#client = client;
15
+ }
16
+ /**
17
+ * Construct invocation objects from JMAP method responses.
18
+ *
19
+ * @param methodResponses The array of JMAP method responses.
20
+ * @param reverseIdMap Map from method call IDs to internal symbols.
21
+ * @returns An InvocationList containing the constructed invocation objects.
22
+ * @throws If no factory is registered for a data type, or there's no factory
23
+ * function for the particular method.
24
+ */
25
+ createInvocations(methodResponses, reverseIdMap) {
26
+ const responses = methodResponses.map(([name, args, methodCallId]) => {
27
+ const id = reverseIdMap.get(methodCallId);
28
+ if (!id)
29
+ this.#logger.warn(`No corresponding ID found for ${methodCallId} in the Request Builder`);
30
+ if (name === "error") {
31
+ this.#logger.warn(`Received error response for invocation with ID: ${methodCallId}`);
32
+ if (!isJMAPResponseInvocationErrorArgs(args)) {
33
+ this.#logger.warn(`Error response missing "type" property for invocation with ID: ${methodCallId}, defaulting to "serverFail"`);
34
+ return new ErrorInvocation({ ...args, type: "serverFail" }, id);
35
+ }
36
+ return new ErrorInvocation(args, id);
37
+ }
38
+ this.#logger.info(`Constructing ${name} invocation with ID: ${methodCallId}`);
39
+ const [dataType, methodName] = name.split("/");
40
+ const factoryFn = this.#client.capabilityRegistry.getInvocationResponseFactory(dataType, methodName);
41
+ if (!factoryFn) {
42
+ this.#logger.error(`No response factory function available for ${name}`);
43
+ throw new Error(`No response factory function available for ${name}`);
44
+ }
45
+ return factoryFn(args, id);
46
+ });
47
+ return new InvocationList(responses, { logger: this.#logger, emitter: this.#emitter });
48
+ }
49
+ }
50
+ //# sourceMappingURL=invocation-factory-manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"invocation-factory-manager.js","sourceRoot":"","sources":["../../../src/invocation-factory/invocation-factory-manager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AACpE,OAAO,EAAE,iCAAiC,EAAE,MAAM,wBAAwB,CAAC;AAE3E,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEtD;;GAEG;AACH,MAAM,OAAO,wBAAwB;IACxB,OAAO,CAAS;IAChB,QAAQ,CAAiB;IACzB,OAAO,CAA+B;IAE/C,YAAY,MAAoC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAiB;QAChF,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IAC1B,CAAC;IAED;;;;;;;;OAQG;IACH,iBAAiB,CAAC,eAAyC,EAAE,YAA6B;QACtF,MAAM,SAAS,GAAG,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC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IAAI,cAAc,CAAC,SAAS,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC3F,CAAC;CACJ"}
@@ -0,0 +1,32 @@
1
+ import type { ErrorInvocation } from "../invocation/error-invocation.js";
2
+ import type { Invocation } from "../invocation/invocation.js";
3
+ import type { BaseInvocationArgs } from "../invocation/types.js";
4
+ import type { ClientContext } from "../jmap-client/types.js";
5
+ import type { HandlerFn, InvocationHandlerMap } from "./types.js";
6
+ export declare class InvocationList<T extends BaseInvocationArgs> implements Iterable<Invocation<T> | ErrorInvocation> {
7
+ #private;
8
+ /**
9
+ * @param invocations The list of invocations (or errors) to manage.
10
+ * @param logger Optional logger instance for logging within the invocation list.
11
+ */
12
+ constructor(invocations: (Invocation<T> | ErrorInvocation)[], { logger }: ClientContext);
13
+ /**
14
+ * @returns An iterator over the invocations and errors in the list.
15
+ */
16
+ [Symbol.iterator](): ArrayIterator<ErrorInvocation | Invocation<T>>;
17
+ /**
18
+ * Get the number of invocations and errors in the list.
19
+ */
20
+ get size(): number;
21
+ /**
22
+ * Dispatch each invocation or error in the list to the appropriate handler function.
23
+ *
24
+ * @param handlers An object mapping method IDs (symbols), method names (e.g., "Mailbox/get"),
25
+ * data types (e.g. "Mailbox"), or the special property `error` to handler functions.
26
+ * - Each handler receives the invocation of the appropriate type.
27
+ * - The `error` property, if present, will be called for error invocations.
28
+ * @param defaultHandler Optional handler for invocations with no registered handler.
29
+ * @returns A promise that resolves when all handlers have been called.
30
+ */
31
+ dispatch(handlers: InvocationHandlerMap, defaultHandler?: HandlerFn): Promise<void>;
32
+ }
@@ -0,0 +1,77 @@
1
+ import { isErrorInvocation } from "../invocation/utils.js";
2
+ export class InvocationList {
3
+ #invocations;
4
+ #logger;
5
+ /**
6
+ * @param invocations The list of invocations (or errors) to manage.
7
+ * @param logger Optional logger instance for logging within the invocation list.
8
+ */
9
+ constructor(invocations, { logger }) {
10
+ this.#invocations = invocations;
11
+ this.#logger = logger;
12
+ }
13
+ /**
14
+ * @returns An iterator over the invocations and errors in the list.
15
+ */
16
+ [Symbol.iterator]() {
17
+ return this.#invocations[Symbol.iterator]();
18
+ }
19
+ /**
20
+ * Get the number of invocations and errors in the list.
21
+ */
22
+ get size() {
23
+ return this.#invocations.length;
24
+ }
25
+ /**
26
+ * Log debug information about handler dispatch.
27
+ * @param invocation The invocation being dispatched
28
+ * @param handlerType The type of handler being used
29
+ */
30
+ #logDispatch(invocation, handlerType) {
31
+ const messages = {
32
+ id: `Dispatching invocation for ${invocation.name} to handler by ID`,
33
+ name: `Dispatching invocation for ${invocation.name} to handler: ${invocation.name}`,
34
+ dataType: `Dispatching invocation for ${invocation.name} to handler: ${invocation.dataType}`,
35
+ default: `Dispatching invocation for ${invocation.name} to default handler`,
36
+ };
37
+ this.#logger.debug(messages[handlerType]);
38
+ }
39
+ /**
40
+ * Dispatch each invocation or error in the list to the appropriate handler function.
41
+ *
42
+ * @param handlers An object mapping method IDs (symbols), method names (e.g., "Mailbox/get"),
43
+ * data types (e.g. "Mailbox"), or the special property `error` to handler functions.
44
+ * - Each handler receives the invocation of the appropriate type.
45
+ * - The `error` property, if present, will be called for error invocations.
46
+ * @param defaultHandler Optional handler for invocations with no registered handler.
47
+ * @returns A promise that resolves when all handlers have been called.
48
+ */
49
+ async dispatch(handlers, defaultHandler) {
50
+ for (const invocation of this) {
51
+ if (isErrorInvocation(invocation)) {
52
+ this.#logger.warn(`Dispatching error invocation: ${invocation.type}`);
53
+ await handlers.error?.(invocation);
54
+ continue;
55
+ }
56
+ let handler = handlers[invocation.id];
57
+ if (handler !== undefined) {
58
+ this.#logDispatch(invocation, "id");
59
+ }
60
+ else if ((handler = handlers[invocation.name]) !== undefined) {
61
+ this.#logDispatch(invocation, "name");
62
+ }
63
+ else if ((handler = handlers[invocation.dataType]) !== undefined) {
64
+ this.#logDispatch(invocation, "dataType");
65
+ }
66
+ else if ((handler = defaultHandler) !== undefined) /* NOSONAR */ {
67
+ this.#logDispatch(invocation, "default");
68
+ }
69
+ else {
70
+ this.#logger.info(`No handler found for invocation: ${invocation.name}`);
71
+ continue;
72
+ }
73
+ await handler(invocation);
74
+ }
75
+ }
76
+ }
77
+ //# sourceMappingURL=invocation-list.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"invocation-list.js","sourceRoot":"","sources":["../../../src/invocation-factory/invocation-list.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAI3D,MAAM,OAAO,cAAc;IACd,YAAY,CAAsC;IAClD,OAAO,CAAS;IAEzB;;;OAGG;IACH,YAAY,WAAgD,EAAE,EAAE,MAAM,EAAiB;QACnF,IAAI,CAAC,YAAY,GAAG,WAAW,CAAC;QAChC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IAC1B,CAAC;IAED;;OAEG;IACH,CAAC,MAAM,CAAC,QAAQ,CAAC;QACb,OAAO,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;IAChD,CAAC;IAED;;OAEG;IACH,IAAI,IAAI;QACJ,OAAO,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC;IACpC,CAAC;IAED;;;;OAIG;IACH,YAAY,CAAC,UAAyB,EAAE,WAAmD;QACvF,MAAM,QAAQ,GAAG;YACb,EAAE,EAAE,8BAA8B,UAAU,CAAC,IAAI,mBAAmB;YACpE,IAAI,EAAE,8BAA8B,UAAU,CAAC,IAAI,gBAAgB,UAAU,CAAC,IAAI,EAAE;YACpF,QAAQ,EAAE,8BAA8B,UAAU,CAAC,IAAI,gBAAgB,UAAU,CAAC,QAAQ,EAAE;YAC5F,OAAO,EAAE,8BAA8B,UAAU,CAAC,IAAI,qBAAqB;SAC9E,CAAC;QACF,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC,CAAC;IAC9C,CAAC;IAED;;;;;;;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@@
1
+ /**
2
+ * A function that handles processing a response invocation.
3
+ */
4
+ export type HandlerFn = (invocation: any) => void | Promise<void>;
5
+ /**
6
+ * A mapping of invocation IDs (symbols), method names (e.g., "Mailbox/get"), data types (e.g. "Mailbox"),
7
+ * or the special property `error` to handler functions.
8
+ */
9
+ export type InvocationHandlerMap = Record<string | symbol, HandlerFn> & {
10
+ error?: HandlerFn;
11
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/invocation-factory/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,252 @@
1
+ /**
2
+ * JMAP Client class implementation
3
+ * @module jmap-client
4
+ * @license MIT
5
+ *
6
+ * @example
7
+ * import { JMAPClient } from 'jmap-client';
8
+ * const transport = // ... create a transport
9
+ *
10
+ * const client = new JMAPClient(transport, {
11
+ * hostname: "api.example.com",
12
+ * });
13
+ *
14
+ * await client.connect();
15
+ *
16
+ * // Do something with the client
17
+ */
18
+ import type { CapabilityDefinition, CapabilityRegistryInterface } from "../capability-registry/types.js";
19
+ import type { Id, JMAPCapability, JMAPServerCapabilities } from "../common/types.js";
20
+ import { RequestBuilder } from "../request-builder/request-builder.js";
21
+ import type { CapabilityRegistrationResult, ConnectionStatus, EventEmitterFn, HeadersInit, JMAPAccount, JMAPClientInterface, JMAPClientOptions, JMAPUploadResponse, Logger, Transport } from "./types.js";
22
+ /**
23
+ * JMAP Client for interacting with a JMAP server.
24
+ *
25
+ * This class manages the connection lifecycle, session state, and provides methods for
26
+ * interacting with the JMAP API, including capability checks, file downloads, and
27
+ * invocation requests and responses.
28
+ */
29
+ export declare class JMAPClient implements JMAPClientInterface<RequestBuilder> {
30
+ #private;
31
+ /**
32
+ * The current connection status of the client.
33
+ */
34
+ get connectionStatus(): ConnectionStatus;
35
+ /**
36
+ * Create a new JMAPClient instance.
37
+ *
38
+ * The provided Transport is responsible for all HTTP request concerns, including authentication,
39
+ * network errors, and any custom headers or request logic required by the server. The client
40
+ * itself does not handle authentication or low-level HTTP details.
41
+ *
42
+ * If the port is not specified, it defaults to 443.
43
+ *
44
+ * @param transport The transport implementation for HTTP requests, including authentication.
45
+ * @param options JMAP client options including hostname, port, headers, logger, and emitter.
46
+ */
47
+ constructor(transport: Transport, { hostname, port, headers, logger, emitter }: JMAPClientOptions);
48
+ /**
49
+ * Register one or more capabilities with the client.
50
+ *
51
+ * When called after the client is connected, each capability's schema is validated against
52
+ * the session data. Capabilities that fail validation are rejected and not registered.
53
+ * When called before connecting, capabilities are registered without validation.
54
+ *
55
+ * If called while the client is in the process of connecting, registration waits for the
56
+ * connection to complete before validating. If the connection fails, capabilities are
57
+ * registered without validation (no session to validate against).
58
+ *
59
+ * @param capabilities The capability definitions to register
60
+ * @returns A result object containing arrays of validation failures (empty arrays if all succeeded)
61
+ */
62
+ registerCapabilities(...capabilities: CapabilityDefinition[]): Promise<CapabilityRegistrationResult>;
63
+ /**
64
+ * Set the hostname for the JMAP server. Only allowed while disconnected.
65
+ *
66
+ * @param hostname The hostname of the JMAP server
67
+ * @throws Error if called after connecting.
68
+ * @returns This client instance (for chaining).
69
+ */
70
+ withHostname(hostname: string): this;
71
+ /**
72
+ * Set the port for the JMAP server. Only allowed while disconnected.
73
+ *
74
+ * @param port Port number.
75
+ * @throws Error if called after connecting.
76
+ * @returns This client instance (for chaining).
77
+ */
78
+ withPort(port: number): this;
79
+ /**
80
+ * Set additional headers for requests.
81
+ *
82
+ * @param headers Additional headers to merge.
83
+ * @returns This client instance (for chaining).
84
+ */
85
+ withHeaders(headers: HeadersInit): this;
86
+ /**
87
+ * Set a custom logger for the client.
88
+ *
89
+ * @param logger The logger instance or function.
90
+ * @returns This client instance (for chaining).
91
+ */
92
+ withLogger(logger: Logger): this;
93
+ /**
94
+ * Set a custom event emitter for the client.
95
+ *
96
+ * @param emitter The event emitter function to handle events.
97
+ * @returns This client instance (for chaining).
98
+ */
99
+ withEmitter(emitter: EventEmitterFn): this;
100
+ /**
101
+ * Connect to the JMAP server and fetch the session object.
102
+ *
103
+ * This method is idempotent: if called multiple times while a connection is already in progress,
104
+ * each call will return a new Promise that resolves or rejects with the same result as the in-progress connection.
105
+ * Only one connection attempt will be made at a time; concurrent calls will not trigger multiple connections.
106
+ *
107
+ * @throws Error if hostname is not set or connection fails.
108
+ */
109
+ connect(signal?: AbortSignal): Promise<void>;
110
+ /**
111
+ * Disconnect from the JMAP server and clear session state.
112
+ *
113
+ * This method is asynchronous and will not set the status to 'disconnected' until all
114
+ * in-flight requests have settled (resolved or rejected) after being aborted.
115
+ * It is idempotent: if already disconnecting, it returns the same promise.
116
+ */
117
+ disconnect(): Promise<void>;
118
+ /**
119
+ * Get the capability registry.
120
+ *
121
+ * This provides read-only access to the registry of JMAP capabilities.
122
+ * To register new capabilities, use the registerCapability method.
123
+ */
124
+ get capabilityRegistry(): CapabilityRegistryInterface;
125
+ /**
126
+ * The server capabilities from the session object, or null if not connected.
127
+ */
128
+ get serverCapabilities(): Readonly<JMAPServerCapabilities> | null;
129
+ /**
130
+ * Determines if a given capability identified by its URN is supported by the server.
131
+ * @param urn The URN of the JMAP capability being queried.
132
+ * @returns True if supported, false otherwise.
133
+ */
134
+ isSupported(urn: JMAPCapability): boolean;
135
+ /**
136
+ * The accounts from the session object, or null if not connected.
137
+ */
138
+ get accounts(): Readonly<Record<Id, JMAPAccount>> | null;
139
+ /**
140
+ * The primary accounts from the session object, or an empty object if not connected.
141
+ */
142
+ get primaryAccounts(): Readonly<Partial<Record<JMAPCapability, Id>>>;
143
+ /**
144
+ * The username from the session object, or an empty string if not connected.
145
+ */
146
+ get username(): string;
147
+ /**
148
+ * The API URL from the session object, or an empty string if not connected.
149
+ */
150
+ get apiUrl(): string;
151
+ /**
152
+ * The download URL template from the session object, or an empty string if not connected.
153
+ */
154
+ get downloadUrl(): string;
155
+ /**
156
+ * The upload URL template from the session object, or an empty string if not connected.
157
+ */
158
+ get uploadUrl(): string;
159
+ /**
160
+ * The event source URL template from the session object, or an empty string if not connected.
161
+ */
162
+ get eventSourceUrl(): string;
163
+ /**
164
+ * Download a file from the server.
165
+ *
166
+ * @param accountId The id of the account to which the record with the blobId belongs.
167
+ * @param blobId The blobId representing the data of the file to download.
168
+ * @param name The name for the file; the server MUST return this as the filename if it sets a Content-Disposition header.
169
+ * @param type The type for the server to set in the Content-Type header of the response; the blobId only represents the binary data and does not have a content-type innately associated with it.
170
+ * @param signal Optional AbortSignal to cancel the request.
171
+ * @throws Error if the client is not connected, downloadUrl is missing from session, accountId is not listed in the session's accounts.
172
+ * @throws JMAPRequestError for JMAP protocol errors (non-200 status codes with {@link https://www.rfc-editor.org/rfc/rfc7807.html RFC 7807} Problem Details).
173
+ * @throws TypeError If parsing the response fails.
174
+ * @throws If a network error, timeout, or other transport failure occurs.
175
+ * @returns A promise that resolves to a Blob containing the data of the file.
176
+ */
177
+ downloadFile(accountId: Id, blobId: Id, name: string, type: string, signal?: AbortSignal): Promise<Blob>;
178
+ /**
179
+ * Upload a file to the server.
180
+ *
181
+ * @param accountId The id of the account to upload to.
182
+ * @param file The file data to upload. Can be a Blob, ArrayBuffer, or File.
183
+ * @param signal Optional AbortSignal to cancel the request.
184
+ * @throws Error if the file size exceeds the maxSizeUpload limit from server capabilities.
185
+ * @throws Error if the client is not connected, uploadUrl is missing from session, or accountId is not listed in the session's accounts.
186
+ * @throws JMAPRequestError for JMAP protocol errors (non-200 status codes with {@link https://www.rfc-editor.org/rfc/rfc7807.html RFC 7807} Problem Details).
187
+ * @throws TypeError If parsing the response fails.
188
+ * @throws If a network error, timeout, or other transport failure occurs.
189
+ * @returns A promise that resolves to the upload response from the server.
190
+ */
191
+ uploadFile(accountId: Id, file: Blob | ArrayBuffer | File, signal?: AbortSignal): Promise<JMAPUploadResponse>;
192
+ /**
193
+ * Get the parsed download URL for a file.
194
+ *
195
+ * @param accountId The id of the account to which the record with the blobId belongs.
196
+ * @param blobId The blobId representing the data of the file to download.
197
+ * @param name The name for the file; the server MUST return this as the filename if it sets a Content-Disposition header.
198
+ * @param type The type for the server to set in the Content-Type header of the response; the blobId only represents the binary data and does not have a content-type innately associated with it.
199
+ * @throws Error if the Client is not connected to a JMAP server
200
+ * @throws Error if downloadUrl is missing from session
201
+ * @throws Error if accountId is not listed in the session's accounts
202
+ * @returns the download URL with variables expanded
203
+ */
204
+ getDownloadUrl(accountId: Id, blobId: Id, name: string, type: string): URL;
205
+ /**
206
+ * Get the parsed upload URL for a file.
207
+ *
208
+ * @param accountId The id of the account to upload the file to.
209
+ * @throws Error if the Client is not connected to a JMAP server
210
+ * @throws Error if uploadUrl is missing from session
211
+ * @throws Error if accountId is not listed in the session's accounts
212
+ * @returns the upload URL with variables expanded
213
+ */
214
+ getUploadUrl(accountId: Id): URL;
215
+ /**
216
+ * Get the parsed event source URL.
217
+ *
218
+ * @param types Either an array of event types to listen for, or "*" to listen for all types.
219
+ * @param closeafter Either "state" (end response after pushing a state event) or "no" (persist connection).
220
+ * @param ping Positive integer value in seconds. If non-zero, the server will send a ping event after this time elapses since the previous event.
221
+ * @throws Error if the Client is not connected to a JMAP server
222
+ * @throws Error if eventSourceUrl is missing from session
223
+ * @throws Error if ping is negative
224
+ * @returns the event source URL with variables expanded
225
+ */
226
+ getEventSourceUrl(types: string[] | "*", closeafter: "state" | "no", ping: number): URL;
227
+ /**
228
+ * Create a new RequestBuilder instance linked to this client.
229
+ *
230
+ * The builder will have access to the client's current capabilities and session state,
231
+ * allowing it to validate requests and enforce server limits.
232
+ *
233
+ * @returns A new RequestBuilder instance
234
+ */
235
+ createRequestBuilder(): RequestBuilder;
236
+ /**
237
+ * Send an API request to the JMAP server.
238
+ *
239
+ * @param jmapRequest The request builder instance.
240
+ * @param signal Optional AbortSignal to cancel the request.
241
+ * @throws Error if the client is not connected, disconnecting, or failed to connect.
242
+ * @throws JMAPRequestError for JMAP protocol errors (non-200 status codes with {@link https://www.rfc-editor.org/rfc/rfc7807.html RFC 7807} Problem Details).
243
+ * @throws TypeError If parsing the response fails.
244
+ * @throws If a network error, timeout, or other transport failure occurs.
245
+ * @returns The parsed response from the server.
246
+ */
247
+ sendAPIRequest(jmapRequest: RequestBuilder, signal?: AbortSignal): Promise<{
248
+ methodResponses: import("../invocation-factory/invocation-list.js").InvocationList<import("../index.js").BaseInvocationArgs>;
249
+ sessionState: string;
250
+ createdIds: import("../common/types.js").IdMap;
251
+ }>;
252
+ }