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 access tokens #39

Merged
merged 1 commit into from
Sep 23, 2020
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ yarn add expo-server-sdk
import { Expo } from 'expo-server-sdk';

// Create a new Expo SDK client
let expo = new Expo();
// optionally providing an access token if you have enabled push security
let expo = new Expo({ accessToken: process.env.EXPO_ACCESS_TOKEN });

// Create the messages that you want to send to clients
let messages = [];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "expo-server-sdk",
"version": "3.5.1",
"version": "3.6.0",
"description": "Server-side library for working with Expo using Node.js",
"main": "build/ExpoClient.js",
"types": "build/ExpoClient.d.ts",
Expand Down
6 changes: 6 additions & 0 deletions src/ExpoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class Expo {

private httpAgent: Agent | undefined;
private limitConcurrentRequests: <T>(thunk: () => Promise<T>) => Promise<T>;
private accessToken: string | undefined;

constructor(options: ExpoClientOptions = {}) {
this.httpAgent = options.httpAgent;
Expand All @@ -46,6 +47,7 @@ export class Expo {
? options.maxConcurrentRequests
: DEFAULT_CONCURRENT_REQUEST_LIMIT
);
this.accessToken = options.accessToken;
}

/**
Expand Down Expand Up @@ -194,6 +196,9 @@ export class Expo {
'Accept-Encoding': 'gzip, deflate',
'User-Agent': `expo-server-sdk-node/${sdkVersion}`,
});
if (this.accessToken) {
requestHeaders.set('Authorization', `Bearer ${this.accessToken}`);
}

if (options.body != null) {
const json = JSON.stringify(options.body);
Expand Down Expand Up @@ -328,6 +333,7 @@ function gzipAsync(data: Buffer): Promise<Buffer> {
export type ExpoClientOptions = {
httpAgent?: Agent;
maxConcurrentRequests?: number;
accessToken?: string;
};

export type ExpoPushToken = string;
Expand Down
22 changes: 21 additions & 1 deletion src/__tests__/ExpoClient-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test('limits the number of concurrent requests', async () => {
});

describe('sending push notification messages', () => {
test('sends requests to the Expo API server', async () => {
test('sends requests to the Expo API server without a supplied access token', async () => {
const mockTickets = [
{ status: 'ok', id: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX' },
{ status: 'ok', id: 'YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY' },
Expand All @@ -38,6 +38,26 @@ describe('sending push notification messages', () => {
expect(options.headers.get('accept-encoding')).toContain('gzip');
expect(options.headers.get('content-type')).toContain('application/json');
expect(options.headers.get('user-agent')).toMatch(/^expo-server-sdk-node\//);
expect(options.headers.get('Authorization')).toBeNull();
});

test('sends requests to the Expo API server with a supplied access token', async () => {
const mockTickets = [
{ status: 'ok', id: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX' },
{ status: 'ok', id: 'YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY' },
];
(fetch as any).mock('https://exp.host/--/api/v2/push/send', { data: mockTickets });

const client = new ExpoClient({ accessToken: 'foobar' });
const tickets = await client.sendPushNotificationsAsync([{ to: 'a' }, { to: 'b' }]);
expect(tickets).toEqual(mockTickets);

const [, options] = (fetch as any).lastCall('https://exp.host/--/api/v2/push/send');
expect(options.headers.get('accept')).toContain('application/json');
expect(options.headers.get('accept-encoding')).toContain('gzip');
expect(options.headers.get('content-type')).toContain('application/json');
expect(options.headers.get('user-agent')).toMatch(/^expo-server-sdk-node\//);
expect(options.headers.get('Authorization')).toContain('Bearer foobar');
});

test('compresses request bodies over 1 KiB', async () => {
Expand Down