-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
43 lines (39 loc) · 1.27 KB
/
index.ts
File metadata and controls
43 lines (39 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import type { LoginReq, LoginResp } from './types';
export async function getAccessToken(): Promise<string> {
const clientId = process.env.UBER_DIRECT_CLIENT_ID;
const clientSecret = process.env.UBER_DIRECT_CLIENT_SECRET;
if (!clientId || !clientSecret) {
throw new Error(
'Must include UBER_DIRECT_CLIENT_ID and UBER_DIRECT_CLIENT_SECRET in environment variables'
);
}
const body: LoginReq = {
client_id: clientId,
client_secret: clientSecret,
grant_type: 'client_credentials',
scope: ['direct.organizations', 'eats.deliveries'].join(' '),
};
try {
const response = await fetch('https://login.uber.com/oauth/v2/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams(body as Record<string, string>).toString(),
});
if (response.ok) {
const data: LoginResp = await response.json();
if (!data.access_token) {
throw new Error('Access token is missing in response');
}
return data.access_token;
} else {
const error = await response.text();
throw new Error(`Failed to fetch access token: ${error}`);
}
} catch (err) {
// TODO: Handle errors better
console.log(err);
throw err;
}
}