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

pass request options to createNotification #59

Open
wants to merge 1 commit into
base: master
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const userClient = new OneSignal.UserClient('userAuthKey', { apiRoot: 'https://o
https://documentation.onesignal.com/reference/create-notification

```ts
.createNotification(body: CreateNotificationBody): Promise<ClientResponse>
.createNotification(body: CreateNotificationBody, customOptions?: ): Promise<ClientResponse>
```

Please read the sections above to learn how to create a `Client` object.
Expand All @@ -137,9 +137,13 @@ const notification = {
]
};

const customOptions = {
timeout: 7000,
}

// using async/await
try {
const response = await client.createNotification(notification);
const response = await client.createNotification(notification, customOptions);
console.log(response.body.id);
} catch (e) {
if (e instanceof OneSignal.HTTPError) {
Expand Down
6 changes: 4 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import {
EditTagsBody,
} from './types';

import { Options as requestOptions } from 'request';

export class Client {
public appId: string;
public apiKey: string;
Expand All @@ -54,11 +56,11 @@ export class Client {
* @param {CreateNotificationBody} body Request body.
* @returns {Promise<Response>} Http response of One Signal server.
*/
public createNotification(body: CreateNotificationBody): Promise<ClientResponse> {
public createNotification(body: CreateNotificationBody, customOptions?: Partial<requestOptions>): Promise<ClientResponse> {
// eslint-disable-next-line @typescript-eslint/camelcase
const postBody = { ...{ [APP_ID_FIELD_NAME]: this.appId }, ...body };
const uri = `${this.options.apiRoot}/${NOTIFICATIONS_PATH}`;
return basicAuthRequest(uri, 'POST', this.apiKey, postBody);
return basicAuthRequest(uri, 'POST', this.apiKey, postBody, customOptions);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const basicAuthRequest = function basicAuthHTTPRequest(
method: string,
authKey: string,
body?: {},
customOptions?: Partial<request.Options>,
): Promise<request.ResponseAsJSON> {
const options: request.Options = {
uri,
Expand All @@ -85,6 +86,7 @@ export const basicAuthRequest = function basicAuthHTTPRequest(
Authorization: `Basic ${authKey}`,
},
json: true,
...customOptions,
};

if (body) {
Expand Down