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