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

Add support for passing in headers to outgoing calls #552

Merged
merged 5 commits into from
Apr 5, 2024
Merged
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/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,16 @@ export default class APIClient {
* The timeout for requests to the Nylas API, in seconds
*/
timeout: number;
/**
* Additional headers to send with outgoing requests
*/
headers: Record<string, string>;

constructor({ apiKey, apiUri, timeout }: Required<NylasConfig>) {
constructor({ apiKey, apiUri, timeout, headers }: Required<NylasConfig>) {
this.apiKey = apiKey;
this.serverUrl = apiUri;
this.timeout = timeout * 1000; // fetch timeout uses milliseconds
this.headers = headers;
}

private setRequestUrl({
Expand Down Expand Up @@ -112,11 +117,17 @@ export default class APIClient {
headers,
overrides,
}: RequestOptionsParams): Record<string, string> {
const mergedHeaders: Record<string, string> = {
...headers,
...this.headers,
...overrides?.headers,
};

return {
Accept: 'application/json',
'User-Agent': `Nylas Node SDK v${SDK_VERSION}`,
Authorization: `Bearer ${overrides?.apiKey || this.apiKey}`,
...headers,
...mergedHeaders,
};
}

Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
* @property apiKey The Nylas API key to use for authentication
* @property apiUri The URL to use for communicating with the Nylas API
* @property timeout The timeout for requests to the Nylas API, in seconds
* @property headers Additional headers to send with outgoing requests
*/
export type NylasConfig = {
apiKey: string;
apiUri?: string;
timeout?: number;
headers?: Record<string, string>;
};

/**
Expand Down
8 changes: 4 additions & 4 deletions src/models/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,10 @@ type When = Time | Timespan | Date | Datespan;
* Type representing the different objects representing time and duration when creating events.
*/
type CreateWhen =
| Omit<Time, 'type'>
| Omit<Timespan, 'type'>
| Omit<Date, 'type'>
| Omit<Datespan, 'type'>;
mrashed-dev marked this conversation as resolved.
Show resolved Hide resolved
| Omit<Time, 'object'>
| Omit<Timespan, 'object'>
| Omit<Date, 'object'>
| Omit<Datespan, 'object'>;

/**
* Enum representing the different types of when objects.
Expand Down
1 change: 1 addition & 0 deletions src/nylas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export default class Nylas {
apiKey: config.apiKey,
apiUri: config.apiUri || DEFAULT_SERVER_URL,
timeout: config.timeout || 90,
headers: config.headers || {},
});

this.applications = new Applications(this.apiClient);
Expand Down
18 changes: 17 additions & 1 deletion tests/apiClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ describe('APIClient', () => {
apiKey: 'test',
apiUri: 'https://test.api.nylas.com',
timeout: 30,
headers: {
'X-SDK-Test-Header': 'This is a test',
},
});

expect(client.apiKey).toBe('test');
expect(client.serverUrl).toBe('https://test.api.nylas.com');
expect(client.timeout).toBe(30000);
expect(client.headers).toEqual({
'X-SDK-Test-Header': 'This is a test',
});
});
});

Expand All @@ -34,6 +40,7 @@ describe('APIClient', () => {
apiKey: 'testApiKey',
apiUri: 'https://api.us.nylas.com',
timeout: 30,
headers: {},
});
});

Expand Down Expand Up @@ -97,13 +104,20 @@ describe('APIClient', () => {

describe('newRequest', () => {
it('should set all the fields properly', () => {
client.headers = {
'global-header': 'global-value',
};

const options: RequestOptionsParams = {
path: '/test',
method: 'POST',
headers: { 'X-SDK-Test-Header': 'This is a test' },
queryParams: { param: 'value' },
body: { id: 'abc123' },
overrides: { apiUri: 'https://override.api.nylas.com' },
overrides: {
apiUri: 'https://override.api.nylas.com',
headers: { override: 'bar' },
},
};
const newRequest = client.newRequest(options);

Expand All @@ -114,6 +128,8 @@ describe('APIClient', () => {
'Content-Type': ['application/json'],
'User-Agent': [`Nylas Node SDK v${SDK_VERSION}`],
'X-SDK-Test-Header': ['This is a test'],
'global-header': ['global-value'],
override: ['bar'],
});
expect(newRequest.url).toEqual(
'https://override.api.nylas.com/test?param=value'
Expand Down
3 changes: 3 additions & 0 deletions tests/resources/applications.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('Applications', () => {
apiKey: 'apiKey',
apiUri: 'https://api.nylas.com',
timeout: 30,
headers: {},
}) as jest.Mocked<APIClient>;

applications = new Applications(apiClient);
Expand All @@ -22,6 +23,7 @@ describe('Applications', () => {
await applications.getDetails({
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});

Expand All @@ -30,6 +32,7 @@ describe('Applications', () => {
path: '/v3/applications',
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});
});
Expand Down
7 changes: 7 additions & 0 deletions tests/resources/attachments.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('Attachments', () => {
apiKey: 'apiKey',
apiUri: 'https://test.api.nylas.com',
timeout: 30,
headers: {},
}) as jest.Mocked<APIClient>;

attachments = new Attachments(apiClient);
Expand All @@ -32,6 +33,7 @@ describe('Attachments', () => {
},
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});

Expand All @@ -43,6 +45,7 @@ describe('Attachments', () => {
},
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});
});
Expand All @@ -58,6 +61,7 @@ describe('Attachments', () => {
},
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});

Expand All @@ -69,6 +73,7 @@ describe('Attachments', () => {
},
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});
});
Expand All @@ -84,6 +89,7 @@ describe('Attachments', () => {
},
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});

Expand All @@ -95,6 +101,7 @@ describe('Attachments', () => {
},
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});
});
Expand Down
3 changes: 3 additions & 0 deletions tests/resources/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('Auth', () => {
apiKey: 'apiKey',
apiUri: 'https://test.api.nylas.com',
timeout: 30,
headers: {},
});

auth = new Auth(apiClient);
Expand Down Expand Up @@ -149,6 +150,7 @@ describe('Auth', () => {
},
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});

Expand All @@ -165,6 +167,7 @@ describe('Auth', () => {
},
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});
});
Expand Down
13 changes: 13 additions & 0 deletions tests/resources/calendars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('Calendars', () => {
apiKey: 'apiKey',
apiUri: 'https://test.api.nylas.com',
timeout: 30,
headers: {},
}) as jest.Mocked<APIClient>;

calendars = new Calendars(apiClient);
Expand All @@ -24,6 +25,7 @@ describe('Calendars', () => {
identifier: 'id123',
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});

Expand All @@ -32,6 +34,7 @@ describe('Calendars', () => {
path: '/v3/grants/id123/calendars',
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});
});
Expand All @@ -44,6 +47,7 @@ describe('Calendars', () => {
calendarId: 'calendar123',
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});

Expand All @@ -52,6 +56,7 @@ describe('Calendars', () => {
path: '/v3/grants/id123/calendars/calendar123',
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});
});
Expand All @@ -67,6 +72,7 @@ describe('Calendars', () => {
},
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});

Expand All @@ -79,6 +85,7 @@ describe('Calendars', () => {
},
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});
});
Expand All @@ -94,6 +101,7 @@ describe('Calendars', () => {
},
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});

Expand All @@ -105,6 +113,7 @@ describe('Calendars', () => {
},
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});
});
Expand All @@ -117,6 +126,7 @@ describe('Calendars', () => {
calendarId: 'calendar123',
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});

Expand All @@ -125,6 +135,7 @@ describe('Calendars', () => {
path: '/v3/grants/id123/calendars/calendar123',
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});
});
Expand Down Expand Up @@ -160,6 +171,7 @@ describe('Calendars', () => {
},
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});

Expand Down Expand Up @@ -193,6 +205,7 @@ describe('Calendars', () => {
},
overrides: {
apiUri: 'https://test.api.nylas.com',
headers: { override: 'bar' },
},
});
});
Expand Down
Loading
Loading