Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace getFirstAccountId #68

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ export class Client {
return Object.keys(session.accounts);
}

/**
* @deprecated Obsoleted by: getPrimaryAccountId
*/
public getFirstAccountId(): string {
const accountIds = this.getAccountIds();

Expand All @@ -102,6 +105,14 @@ export class Client {
return accountIds[0];
}

public getPrimaryAccountId(capability = 'urn:ietf:params:jmap:mail'): string {
const primaryAccounts = this.getSession().primaryAccounts;
if (primaryAccounts && capability in primaryAccounts) {
return primaryAccounts[capability];
}
throw new Error('No primary account available for $capability in this session');
}

public mailbox_get(args: IMailboxGetArguments): Promise<IMailboxGetResponse> {
return this.request<IMailboxGetResponse>('Mailbox/get', args);
}
Expand Down Expand Up @@ -158,7 +169,7 @@ export class Client {

public upload(buffer: ArrayBuffer, type = 'application/octet-stream'): Promise<IUploadResponse> {
const uploadUrl = this.getSession().uploadUrl;
const accountId = this.getFirstAccountId();
const accountId = this.getPrimaryAccountId();
const requestHeaders = {
...this.httpHeaders,
'Content-Type': type,
Expand Down Expand Up @@ -200,7 +211,7 @@ export class Client {
? input
: {
...input,
accountId: this.getFirstAccountId(),
accountId: this.getPrimaryAccountId(),
};
}

Expand Down
48 changes: 24 additions & 24 deletions tests/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('jmap-client-ts', () => {

it('should have mailbox_get working', async () => {
const response = await client.mailbox_get({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
ids: null,
});

Expand All @@ -107,17 +107,17 @@ describe('jmap-client-ts', () => {

it('should have mailbox_changes working', async () => {
const getResponse = await client.mailbox_get({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
ids: null,
});

const changesResponse = await client.mailbox_changes({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
sinceState: getResponse.state,
});

expect(changesResponse).toMatchObject<IMailboxChangesResponse>({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
oldState: getResponse.state,
newState: getResponse.state,
hasMoreChanges: false,
Expand All @@ -130,19 +130,19 @@ describe('jmap-client-ts', () => {

it('should have email_query working', async () => {
const response = await client.email_query({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
});

expect(response.accountId).toBeDefined();
});

it('should have email_get working', async () => {
const emailQueryResponse = await client.email_query({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
});

const emailGetResponse = await client.email_get({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
ids: emailQueryResponse.ids,
});

Expand All @@ -151,17 +151,17 @@ describe('jmap-client-ts', () => {

it('should have email_changes working', async () => {
const getResponse = await client.email_get({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
ids: [],
});

const changesResponse = await client.email_changes({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
sinceState: getResponse.state,
});

expect(changesResponse).toMatchObject<IEmailChangesResponse>({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
oldState: getResponse.state,
newState: getResponse.state,
hasMoreChanges: false,
Expand All @@ -171,7 +171,7 @@ describe('jmap-client-ts', () => {
});

const getMailboxesResponse = await client.mailbox_get({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
ids: null,
});

Expand All @@ -180,7 +180,7 @@ describe('jmap-client-ts', () => {
);

const emailCreatedResponse = await client.email_set({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
create: {
emailToCreateId: {
mailboxIds: {
Expand All @@ -193,12 +193,12 @@ describe('jmap-client-ts', () => {
const emailCreatedId = emailCreatedResponse.created?.emailToCreateId.id as string;

const newChangesResponse = await client.email_changes({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
sinceState: getResponse.state,
});

expect(newChangesResponse).toMatchObject<IEmailChangesResponse>({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
oldState: getResponse.state,
newState: emailCreatedResponse.newState,
hasMoreChanges: false,
Expand All @@ -210,7 +210,7 @@ describe('jmap-client-ts', () => {

it('should have email_set working', async () => {
const getMailboxesResponse = await client.mailbox_get({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
ids: null,
});

Expand All @@ -219,7 +219,7 @@ describe('jmap-client-ts', () => {
);

const emailSetResponse = await client.email_set({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
create: {
emailCreated: {
mailboxIds: {
Expand All @@ -245,7 +245,7 @@ describe('jmap-client-ts', () => {
it('should create mailbox', async () => {
const id = '674cc24095db49ce';
const response = await client.mailbox_set({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
create: {
[id]: {
name: 'mailbox1',
Expand All @@ -265,7 +265,7 @@ describe('jmap-client-ts', () => {

it('should submit email', async () => {
const getMailboxesResponse = await client.mailbox_get({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
ids: null,
});

Expand All @@ -274,7 +274,7 @@ describe('jmap-client-ts', () => {
);

const emailSetResponse = await client.email_set({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
create: {
emailCreated: {
mailboxIds: {
Expand Down Expand Up @@ -306,7 +306,7 @@ describe('jmap-client-ts', () => {

const id = '674cc24095db49ce';
const response = await client.emailSubmission_set({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
create: {
[id]: {
emailId: emailSetResponse.created!.emailCreated.id,
Expand All @@ -332,7 +332,7 @@ describe('jmap-client-ts', () => {
expect(uploadResponse.blobId).toBeDefined();

const getMailboxesResponse = await client.mailbox_get({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
ids: null,
});

Expand All @@ -353,7 +353,7 @@ describe('jmap-client-ts', () => {
const dateString = new Date().toISOString();

let importResponse = await client.email_import({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
ifInState: null,
emails: {
[importId1]: {
Expand Down Expand Up @@ -394,7 +394,7 @@ describe('jmap-client-ts', () => {
const importId3 = '7654321';
const uploadResponse3 = await client.upload(readFileSync('./tests/3.eml'), 'message/rfc822');
importResponse = await client.email_import({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
ifInState: null,
emails: {
[importId3]: {
Expand All @@ -415,7 +415,7 @@ describe('jmap-client-ts', () => {
const emailId3 = created[importId3].id;

const threadGetResponse = await client.thread_get({
accountId: client.getAccountIds()[0],
accountId: client.getPrimaryAccountId(),
ids: [threadId],
});

Expand Down