1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Get Company-Specific Open API Documentation |
8 | * The company-specific Open API endpoint allows the client to GET an Open API document for the Paylocity API that is customized with company-specific resource schemas. |
9 | */ |
10 | export async function main(auth: Paylocity, companyId: string) { |
11 | const url = new URL(`https://dc1prodgwext.paylocity.com/api/v2/companies/${companyId}/openapi`) |
12 |
|
13 | const response = await fetch(url, { |
14 | method: 'GET', |
15 | headers: { |
16 | Authorization: |
17 | 'Bearer ' + |
18 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
19 | }, |
20 | body: undefined |
21 | }) |
22 | if (!response.ok) { |
23 | const text = await response.text() |
24 | throw new Error(`${response.status} ${text}`) |
25 | } |
26 | return await response.text() |
27 | } |
28 |
|
29 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
30 | const params = new URLSearchParams({ |
31 | grant_type: 'client_credentials', |
32 | client_id: auth.clientId, |
33 | client_secret: auth.clientSecret |
34 | }) |
35 |
|
36 | const response = await fetch(tokenUrl, { |
37 | method: 'POST', |
38 | headers: { |
39 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
40 | 'Content-Type': 'application/x-www-form-urlencoded' |
41 | }, |
42 | body: params.toString() |
43 | }) |
44 |
|
45 | if (!response.ok) { |
46 | const text = await response.text() |
47 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
48 | } |
49 |
|
50 | const data = await response.json() |
51 | return data.access_token |
52 | } |
53 |
|