Skip to content

Commit

Permalink
Merge pull request linagora#54 from alagane/throw-jmap-method-errors
Browse files Browse the repository at this point in the history
Throw jmap method errors
  • Loading branch information
fabienmoyon authored Jun 14, 2021
2 parents a89e845 + 1b591df commit 1441491
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
IEmailSubmissionChangesResponse,
IEmailChangesArguments,
IEmailChangesResponse,
IInvocation,
} from './types';

export class Client {
Expand Down Expand Up @@ -143,7 +144,7 @@ export class Client {
return this.transport
.post<{
sessionState: string;
methodResponses: [[IMethodName, ResponseType, string]];
methodResponses: IInvocation<ResponseType>[];
}>(
apiUrl,
{
Expand All @@ -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<U extends IReplaceableAccountId>(input: U): U {
Expand Down
23 changes: 19 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ export type IMethodName =
| 'EmailSubmission/changes'
| 'EmailSubmission/set';

export type IErrorName = 'error';

export type IInvocationName = IMethodName | IErrorName;

/**
* See https://jmap.io/spec-core.html#the-invocation-data-type
* [ name, arguments, id ]
*/
export type IMethodCall = [IMethodName, IArguments, string];
export type IInvocation<ArgumentsType> = [
name: IInvocationName,
arguments: ArgumentsType,
methodCallId: string,
];

export type IEntityProperties = IMailboxProperties | IEmailProperties | IEmailSubmissionProperties;

Expand All @@ -26,7 +33,8 @@ export type IFilterCondition = IMailboxFilterCondition | IEmailFilterCondition;
export type IArguments =
| IGetArguments<IEntityProperties>
| ISetArguments<IEntityProperties>
| IQueryArguments<IEmailFilterCondition>;
| IQueryArguments<IEmailFilterCondition>
| IChangesArguments;
export interface IReplaceableAccountId {
/**
* If null, the library will replace its value by default account id.
Expand Down Expand Up @@ -151,7 +159,7 @@ export interface IComparator {
*/
export interface IRequest {
using: string[];
methodCalls: IMethodCall[];
methodCalls: IInvocation<IArguments>[];
createdIds?: { [creationId: string]: string };
}

Expand Down Expand Up @@ -326,6 +334,13 @@ export type IMailboxSetArguments = ISetArguments<IMailboxProperties>;

export type IMailboxSetResponse = ISetResponse<IMailboxProperties>;

/**
* 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
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit 1441491

Please sign in to comment.