From 91f5bdee9378e8fe3f6fe61fff339ac0a595cfec Mon Sep 17 00:00:00 2001 From: Alexandre Lagane Date: Thu, 29 Apr 2021 15:47:27 +0200 Subject: [PATCH 1/3] Throw jmap method errors Related to #53 --- src/index.ts | 13 +++++++++++-- src/types.ts | 7 +++++++ tests/integration.spec.ts | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2fe4e23..d56ab64 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,6 +24,7 @@ import { IEmailSubmissionChangesResponse, IEmailChangesArguments, IEmailChangesResponse, + IError, } from './types'; export class Client { @@ -143,7 +144,7 @@ export class Client { return this.transport .post<{ sessionState: string; - methodResponses: [[IMethodName, ResponseType, string]]; + methodResponses: ([IMethodName, ResponseType, string] | ['error', IError, string])[]; }>( apiUrl, { @@ -152,7 +153,15 @@ export class Client { }, this.httpHeaders, ) - .then(response => response.methodResponses[0][1]); + .then(response => { + const methodResponse = response.methodResponses[0]; + + if (methodResponse[0] === 'error') { + throw methodResponse[1]; + } + + return methodResponse[1]; + }); } private replaceAccountId(input: U): U { diff --git a/src/types.ts b/src/types.ts index 1aa2e38..4eeeb5c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -326,6 +326,13 @@ export type IMailboxSetArguments = ISetArguments; export type IMailboxSetResponse = ISetResponse; +/** + * See https://jmap.io/spec-core.html#method-level-errors + */ +export interface IError { + type: IErrorType; +} + /** * See https://jmap.io/spec-core.html#creation-of-jmap-error-codes-registry */ diff --git a/tests/integration.spec.ts b/tests/integration.spec.ts index 88f235f..f44f731 100644 --- a/tests/integration.spec.ts +++ b/tests/integration.spec.ts @@ -55,6 +55,20 @@ describe('jmap-client-ts', () => { await container.stop(); }); + it('should get error correctly', async () => { + let error = null; + try { + await client.mailbox_get({ + accountId: 'unknown-account-id', + ids: null, + }); + } catch (e) { + error = e; + } + + expect(error.type).toEqual('accountNotFound'); + }); + it('should have mailbox_get working', async () => { const response = await client.mailbox_get({ accountId: client.getAccountIds()[0], From 41f2978d81f9d6587a8ab9c6dac40b1c95b93367 Mon Sep 17 00:00:00 2001 From: Alexandre Lagane Date: Fri, 30 Apr 2021 11:15:55 +0200 Subject: [PATCH 2/3] Rename IMethodCall to IInvocation and refactor --- src/index.ts | 4 ++-- src/types.ts | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index d56ab64..6b4db0e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,7 +24,7 @@ import { IEmailSubmissionChangesResponse, IEmailChangesArguments, IEmailChangesResponse, - IError, + IInvocation, } from './types'; export class Client { @@ -144,7 +144,7 @@ export class Client { return this.transport .post<{ sessionState: string; - methodResponses: ([IMethodName, ResponseType, string] | ['error', IError, string])[]; + methodResponses: IInvocation[]; }>( apiUrl, { diff --git a/src/types.ts b/src/types.ts index 4eeeb5c..f5dc25e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -10,11 +10,15 @@ export type IMethodName = | 'EmailSubmission/changes' | 'EmailSubmission/set'; +export type IErrorName = 'error'; + /** * See https://jmap.io/spec-core.html#the-invocation-data-type - * [ name, arguments, id ] + * [ name, arguments, methodCallId ] */ -export type IMethodCall = [IMethodName, IArguments, string]; +export type IInvocation = + | [IMethodName, ArgumentsType, string] + | [IErrorName, IError, string]; export type IEntityProperties = IMailboxProperties | IEmailProperties | IEmailSubmissionProperties; @@ -26,7 +30,8 @@ export type IFilterCondition = IMailboxFilterCondition | IEmailFilterCondition; export type IArguments = | IGetArguments | ISetArguments - | IQueryArguments; + | IQueryArguments + | IChangesArguments; export interface IReplaceableAccountId { /** * If null, the library will replace its value by default account id. @@ -151,7 +156,7 @@ export interface IComparator { */ export interface IRequest { using: string[]; - methodCalls: IMethodCall[]; + methodCalls: IInvocation[]; createdIds?: { [creationId: string]: string }; } From 1b591df09d48aaee29e4a7d2568ab1abf40abaf1 Mon Sep 17 00:00:00 2001 From: Alexandre Lagane Date: Mon, 3 May 2021 13:33:23 +0200 Subject: [PATCH 3/3] fixup! Rename IMethodCall to IInvocation and refactor --- src/types.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/types.ts b/src/types.ts index f5dc25e..ca20d83 100644 --- a/src/types.ts +++ b/src/types.ts @@ -12,13 +12,16 @@ export type IMethodName = export type IErrorName = 'error'; +export type IInvocationName = IMethodName | IErrorName; + /** * See https://jmap.io/spec-core.html#the-invocation-data-type - * [ name, arguments, methodCallId ] */ -export type IInvocation = - | [IMethodName, ArgumentsType, string] - | [IErrorName, IError, string]; +export type IInvocation = [ + name: IInvocationName, + arguments: ArgumentsType, + methodCallId: string, +]; export type IEntityProperties = IMailboxProperties | IEmailProperties | IEmailSubmissionProperties;