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