Skip to content
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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,34 @@ const deliveriesClient = createDeliveriesClient(token);

You can use this client for subsequent requests.

### Using Sandbox Environment

Both the Organizations and Deliveries clients support connecting to the Uber sandbox environment for testing purposes. By default, clients connect to the production API (`api.uber.com`). You can specify the sandbox environment by passing options to the client constructors:

#### Deliveries Client with Sandbox

```js
import { getAccessToken } from 'uber-direct/auth';
import { createDeliveriesClient } from 'uber-direct/deliveries';

const token = await getAccessToken();
const deliveriesClient = createDeliveriesClient(token, customerId, {
environment: 'sandbox'
});
```

#### Organizations Client with Sandbox

```js
import { getAccessToken } from 'uber-direct/auth';
import { createOrganizationsClient } from 'uber-direct/organizations';

const token = await getAccessToken();
const organizationsClient = createOrganizationsClient(token, {
environment: 'sandbox'
});
```

#### Create Quote

```js
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/deliveries/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,24 @@ describe('DeliveriesClient', () => {
`https://api.uber.com/v1/customers/${customerId}`
);
});

it('success - should use sandbox URL when environment is set to sandbox', () => {
const deliveriesClient = createDeliveriesClient(accessToken, customerId, {
environment: 'sandbox'
});

expect(deliveriesClient).toBeDefined();
expect(deliveriesClient.baseURL).toEqual(
`https://sandbox-api.uber.com/v1/customers/${customerId}`
);
});

it('success - should use production URL by default', () => {
const deliveriesClient = createDeliveriesClient(accessToken, customerId);

expect(deliveriesClient).toBeDefined();
expect(deliveriesClient.baseURL).toEqual(
`https://api.uber.com/v1/customers/${customerId}`
);
});
});
25 changes: 25 additions & 0 deletions src/__tests__/organizations/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,29 @@ describe('OrganizationsClient', () => {

expect(organizationsClient).toBeDefined();
});

it('success - should use production URL by default', () => {
const organizationsClient = createOrganizationsClient(accessToken);

expect(organizationsClient).toBeDefined();
expect(organizationsClient.baseURL).toEqual('https://api.uber.com/v1/direct');
});

it('success - should use sandbox URL when environment is set to sandbox', () => {
const organizationsClient = createOrganizationsClient(accessToken, {
environment: 'sandbox'
});

expect(organizationsClient).toBeDefined();
expect(organizationsClient.baseURL).toEqual('https://sandbox-api.uber.com/v1/direct');
});

it('success - should use production URL when environment is set to production', () => {
const organizationsClient = createOrganizationsClient(accessToken, {
environment: 'production'
});

expect(organizationsClient).toBeDefined();
expect(organizationsClient.baseURL).toEqual('https://api.uber.com/v1/direct');
});
});
16 changes: 16 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type Environment = 'production' | 'sandbox';

export interface ClientOptions {
/**
* The environment to use for API requests.
* - 'production': api.uber.com (default)
* - 'sandbox': api-sandbox.uber.com
*/
environment?: Environment;
}

export const getBaseURL = (environment: Environment = 'production'): string => {
return environment === 'sandbox'
? 'https://sandbox-api.uber.com'
: 'https://api.uber.com';
};
13 changes: 8 additions & 5 deletions src/deliveries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ import type {
UpdateDeliveryReq,
} from './types';
import { fetchData, makeQueryString } from '../utils';
import { getBaseURL, type ClientOptions } from '../config';

export class DeliveriesClient {
accessToken: string;
baseURL: string;
customerID: string;

constructor(accessToken: string, customerID?: string) {
constructor(accessToken: string, customerId?: string, options?: ClientOptions) {
this.accessToken = accessToken;

const cusID = customerID || process.env.UBER_DIRECT_CUSTOMER_ID;
const cusID = customerId || process.env.UBER_DIRECT_CUSTOMER_ID;

if (!cusID) {
throw new Error(
'Must include UBER_DIRECT_CUSTOMER_ID in environment variables or pass it in as an argument'
);
}
this.customerID = cusID;
this.baseURL = `https://api.uber.com/v1/customers/${this.customerID}`;
const baseURL = getBaseURL(options?.environment);
this.baseURL = `${baseURL}/v1/customers/${this.customerID}`;
}

async createQuote(req: DeliveryQuoteReq) {
Expand Down Expand Up @@ -89,7 +91,8 @@ export class DeliveriesClient {

export const createDeliveriesClient = (
accessToken: string,
customerID?: string
customerId?: string,
options?: ClientOptions
) => {
return new DeliveriesClient(accessToken, customerID);
return new DeliveriesClient(accessToken, customerId, options);
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { getAccessToken } from './auth';
export { DeliveriesClient, createDeliveriesClient } from './deliveries';
export { OrganizationsClient, createOrganizationsClient } from './organizations';
export { type ClientOptions, type Environment } from './config';
13 changes: 9 additions & 4 deletions src/organizations/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { CreateOrgReq } from './types';
import type { InviteMemberReq } from './types';
import { fetchData } from '../utils';
import { getBaseURL, type ClientOptions } from '../config';

export class OrganizationsClient {
accessToken: string;
baseURL: string;

constructor(accessToken: string) {
constructor(accessToken: string, options?: ClientOptions) {
this.accessToken = accessToken;
this.baseURL = 'https://api.uber.com/v1/direct';
const baseURL = getBaseURL(options?.environment);
this.baseURL = `${baseURL}/v1/direct`;
}

async createOrganization(req: CreateOrgReq) {
Expand All @@ -26,6 +28,9 @@ export class OrganizationsClient {

}

export const createOrganizationsClient = (accessToken: string) => {
return new OrganizationsClient(accessToken);
export const createOrganizationsClient = (
accessToken: string,
options?: ClientOptions
) => {
return new OrganizationsClient(accessToken, options);
};