@tryfinch/finch-api 5.2.0 → 5.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/core.d.ts +1 -1
  3. package/core.d.ts.map +1 -1
  4. package/core.js +31 -22
  5. package/core.js.map +1 -1
  6. package/core.mjs +32 -23
  7. package/core.mjs.map +1 -1
  8. package/error.d.ts +3 -1
  9. package/error.d.ts.map +1 -1
  10. package/error.js +5 -2
  11. package/error.js.map +1 -1
  12. package/error.mjs +3 -1
  13. package/error.mjs.map +1 -1
  14. package/index.d.mts +6 -1
  15. package/index.d.ts +6 -1
  16. package/index.d.ts.map +1 -1
  17. package/index.js +5 -2
  18. package/index.js.map +1 -1
  19. package/index.mjs +4 -1
  20. package/index.mjs.map +1 -1
  21. package/package.json +1 -1
  22. package/resources/hris/employments.d.ts +4 -0
  23. package/resources/hris/employments.d.ts.map +1 -1
  24. package/resources/hris/employments.js.map +1 -1
  25. package/resources/hris/employments.mjs.map +1 -1
  26. package/resources/hris/hris.d.ts +1 -1
  27. package/resources/hris/hris.d.ts.map +1 -1
  28. package/resources/index.d.ts +1 -1
  29. package/resources/index.d.ts.map +1 -1
  30. package/resources/index.js +3 -1
  31. package/resources/index.js.map +1 -1
  32. package/resources/index.mjs +1 -0
  33. package/resources/index.mjs.map +1 -1
  34. package/resources/request-forwarding.d.ts +99 -0
  35. package/resources/request-forwarding.d.ts.map +1 -0
  36. package/resources/request-forwarding.js +17 -0
  37. package/resources/request-forwarding.js.map +1 -0
  38. package/resources/request-forwarding.mjs +13 -0
  39. package/resources/request-forwarding.mjs.map +1 -0
  40. package/src/core.ts +39 -28
  41. package/src/error.ts +3 -1
  42. package/src/index.ts +7 -0
  43. package/src/resources/hris/employments.ts +5 -0
  44. package/src/resources/hris/hris.ts +1 -1
  45. package/src/resources/index.ts +5 -1
  46. package/src/resources/request-forwarding.ts +121 -0
  47. package/src/version.ts +1 -1
  48. package/version.d.ts +1 -1
  49. package/version.js +1 -1
  50. package/version.mjs +1 -1
package/src/core.ts CHANGED
@@ -1,5 +1,11 @@
1
1
  import { VERSION } from './version';
2
- import { APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError } from './error';
2
+ import {
3
+ FinchError,
4
+ APIError,
5
+ APIConnectionError,
6
+ APIConnectionTimeoutError,
7
+ APIUserAbortError,
8
+ } from './error';
3
9
  import {
4
10
  kind as shimsKind,
5
11
  type Readable,
@@ -433,7 +439,7 @@ export abstract class APIClient {
433
439
  if (value === null) {
434
440
  return `${encodeURIComponent(key)}=`;
435
441
  }
436
- throw new Error(
442
+ throw new FinchError(
437
443
  `Cannot stringify type ${typeof value}; Expected string, number, boolean, or null. If you need to pass nested query parameters, you can manually encode them, e.g. { query: { 'foo[key1]': value1, 'foo[key2]': value2 } }, and please open a GitHub issue requesting better support for your use case.`,
438
444
  );
439
445
  })
@@ -496,32 +502,37 @@ export abstract class APIClient {
496
502
  retriesRemaining -= 1;
497
503
 
498
504
  // About the Retry-After header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
499
- //
500
- // TODO: we may want to handle the case where the header is using the http-date syntax: "Retry-After: <http-date>".
501
- // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After#syntax for details.
502
- const retryAfter = parseInt(responseHeaders?.['retry-after'] || '');
505
+ let timeoutMillis: number | undefined;
506
+ const retryAfterHeader = responseHeaders?.['retry-after'];
507
+ if (retryAfterHeader) {
508
+ const timeoutSeconds = parseInt(retryAfterHeader);
509
+ if (!Number.isNaN(timeoutSeconds)) {
510
+ timeoutMillis = timeoutSeconds * 1000;
511
+ } else {
512
+ timeoutMillis = Date.parse(retryAfterHeader) - Date.now();
513
+ }
514
+ }
503
515
 
504
- const maxRetries = options.maxRetries ?? this.maxRetries;
505
- const timeout = this.calculateRetryTimeoutSeconds(retriesRemaining, retryAfter, maxRetries) * 1000;
506
- await sleep(timeout);
516
+ // If the API asks us to wait a certain amount of time (and it's a reasonable amount),
517
+ // just do what it says, but otherwise calculate a default
518
+ if (
519
+ !timeoutMillis ||
520
+ !Number.isInteger(timeoutMillis) ||
521
+ timeoutMillis <= 0 ||
522
+ timeoutMillis > 60 * 1000
523
+ ) {
524
+ const maxRetries = options.maxRetries ?? this.maxRetries;
525
+ timeoutMillis = this.calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries);
526
+ }
527
+ await sleep(timeoutMillis);
507
528
 
508
529
  return this.makeRequest(options, retriesRemaining);
509
530
  }
510
531
 
511
- private calculateRetryTimeoutSeconds(
512
- retriesRemaining: number,
513
- retryAfter: number,
514
- maxRetries: number,
515
- ): number {
532
+ private calculateDefaultRetryTimeoutMillis(retriesRemaining: number, maxRetries: number): number {
516
533
  const initialRetryDelay = 0.5;
517
534
  const maxRetryDelay = 2;
518
535
 
519
- // If the API asks us to wait a certain amount of time (and it's a reasonable amount),
520
- // just do what it says.
521
- if (Number.isInteger(retryAfter) && retryAfter <= 60) {
522
- return retryAfter;
523
- }
524
-
525
536
  const numRetries = maxRetries - retriesRemaining;
526
537
 
527
538
  // Apply exponential backoff, but not more than the max.
@@ -530,7 +541,7 @@ export abstract class APIClient {
530
541
  // Apply some jitter, plus-or-minus half a second.
531
542
  const jitter = Math.random() - 0.5;
532
543
 
533
- return sleepSeconds + jitter;
544
+ return (sleepSeconds + jitter) * 1000;
534
545
  }
535
546
 
536
547
  private getUserAgent(): string {
@@ -592,7 +603,7 @@ export abstract class AbstractPage<Item> implements AsyncIterable<Item> {
592
603
  async getNextPage(): Promise<this> {
593
604
  const nextInfo = this.nextPageInfo();
594
605
  if (!nextInfo) {
595
- throw new Error(
606
+ throw new FinchError(
596
607
  'No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.',
597
608
  );
598
609
  }
@@ -918,10 +929,10 @@ export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve
918
929
 
919
930
  const validatePositiveInteger = (name: string, n: unknown): number => {
920
931
  if (typeof n !== 'number' || !Number.isInteger(n)) {
921
- throw new Error(`${name} must be an integer`);
932
+ throw new FinchError(`${name} must be an integer`);
922
933
  }
923
934
  if (n < 0) {
924
- throw new Error(`${name} must be a positive integer`);
935
+ throw new FinchError(`${name} must be a positive integer`);
925
936
  }
926
937
  return n;
927
938
  };
@@ -932,7 +943,7 @@ export const castToError = (err: any): Error => {
932
943
  };
933
944
 
934
945
  export const ensurePresent = <T>(value: T | null | undefined): T => {
935
- if (value == null) throw new Error(`Expected a value to be given but received ${value} instead.`);
946
+ if (value == null) throw new FinchError(`Expected a value to be given but received ${value} instead.`);
936
947
  return value;
937
948
  };
938
949
 
@@ -955,14 +966,14 @@ export const coerceInteger = (value: unknown): number => {
955
966
  if (typeof value === 'number') return Math.round(value);
956
967
  if (typeof value === 'string') return parseInt(value, 10);
957
968
 
958
- throw new Error(`Could not coerce ${value} (type: ${typeof value}) into a number`);
969
+ throw new FinchError(`Could not coerce ${value} (type: ${typeof value}) into a number`);
959
970
  };
960
971
 
961
972
  export const coerceFloat = (value: unknown): number => {
962
973
  if (typeof value === 'number') return value;
963
974
  if (typeof value === 'string') return parseFloat(value);
964
975
 
965
- throw new Error(`Could not coerce ${value} (type: ${typeof value}) into a number`);
976
+ throw new FinchError(`Could not coerce ${value} (type: ${typeof value}) into a number`);
966
977
  };
967
978
 
968
979
  export const coerceBoolean = (value: unknown): boolean => {
@@ -1066,5 +1077,5 @@ export const toBase64 = (str: string | null | undefined): string => {
1066
1077
  return btoa(str);
1067
1078
  }
1068
1079
 
1069
- throw new Error('Cannot generate b64 string; Expected `Buffer` or `btoa` to be defined');
1080
+ throw new FinchError('Cannot generate b64 string; Expected `Buffer` or `btoa` to be defined');
1070
1081
  };
package/src/error.ts CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
  import { castToError, Headers } from './core';
4
4
 
5
- export class APIError extends Error {
5
+ export class FinchError extends Error {}
6
+
7
+ export class APIError extends FinchError {
6
8
  readonly status: number | undefined;
7
9
  readonly headers: Headers | undefined;
8
10
  readonly error: Object | undefined;
package/src/index.ts CHANGED
@@ -133,6 +133,7 @@ export class Finch extends Core.APIClient {
133
133
  providers: API.Providers = new API.Providers(this);
134
134
  account: API.Account = new API.Account(this);
135
135
  webhooks: API.Webhooks = new API.Webhooks(this);
136
+ requestForwarding: API.RequestForwarding = new API.RequestForwarding(this);
136
137
 
137
138
  /**
138
139
  * Returns an access token for the Finch API given an authorization code. An
@@ -220,6 +221,7 @@ export class Finch extends Core.APIClient {
220
221
 
221
222
  static Finch = this;
222
223
 
224
+ static FinchError = Errors.FinchError;
223
225
  static APIError = Errors.APIError;
224
226
  static APIConnectionError = Errors.APIConnectionError;
225
227
  static APIConnectionTimeoutError = Errors.APIConnectionTimeoutError;
@@ -235,6 +237,7 @@ export class Finch extends Core.APIClient {
235
237
  }
236
238
 
237
239
  export const {
240
+ FinchError,
238
241
  APIError,
239
242
  APIConnectionError,
240
243
  APIConnectionTimeoutError,
@@ -284,6 +287,10 @@ export namespace Finch {
284
287
  export import Introspection = API.Introspection;
285
288
 
286
289
  export import Webhooks = API.Webhooks;
290
+
291
+ export import RequestForwarding = API.RequestForwarding;
292
+ export import RequestForwardingForwardResponse = API.RequestForwardingForwardResponse;
293
+ export import RequestForwardingForwardParams = API.RequestForwardingForwardParams;
287
294
  }
288
295
 
289
296
  export default Finch;
@@ -98,6 +98,11 @@ export interface EmploymentData {
98
98
  */
99
99
  pay_group_ids?: Array<string> | null;
100
100
 
101
+ /**
102
+ * The source system's unique employment identifier for this individual
103
+ */
104
+ source_id?: string | null;
105
+
101
106
  start_date?: string | null;
102
107
 
103
108
  /**
@@ -98,7 +98,7 @@ export interface Money {
98
98
  /**
99
99
  * Amount for money object (in cents)
100
100
  */
101
- amount?: number;
101
+ amount?: number | null;
102
102
 
103
103
  currency?: string;
104
104
  }
@@ -1,7 +1,11 @@
1
1
  // File generated from our OpenAPI spec by Stainless.
2
2
 
3
- export {} from './top-level';
4
3
  export { DisconnectResponse, Introspection, Account } from './account';
5
4
  export { Income, Location, Money, Paging, HRIS } from './hris/hris';
6
5
  export { Provider, ProvidersSinglePage, Providers } from './providers';
6
+ export {
7
+ RequestForwardingForwardResponse,
8
+ RequestForwardingForwardParams,
9
+ RequestForwarding,
10
+ } from './request-forwarding';
7
11
  export { Webhooks } from './webhooks';
@@ -0,0 +1,121 @@
1
+ // File generated from our OpenAPI spec by Stainless.
2
+
3
+ import * as Core from "../ch/finch-api/core";
4
+ import { APIResource } from "../ch/finch-api/resource";
5
+ import * as API from './index';
6
+
7
+ export class RequestForwarding extends APIResource {
8
+ /**
9
+ * The Forward API allows you to make direct requests to an employment system.
10
+ */
11
+ forward(
12
+ body: RequestForwardingForwardParams,
13
+ options?: Core.RequestOptions,
14
+ ): Core.APIPromise<RequestForwardingForwardResponse> {
15
+ return this.post('/forward', { body, ...options });
16
+ }
17
+ }
18
+
19
+ export interface RequestForwardingForwardResponse {
20
+ /**
21
+ * A string representation of the HTTP response body of the forwarded request’s
22
+ * response received from the underlying integration’s API. This field may be null
23
+ * in the case where the upstream system’s response is empty.
24
+ */
25
+ data: string | null;
26
+
27
+ /**
28
+ * The HTTP headers of the forwarded request’s response, exactly as received from
29
+ * the underlying integration’s API.
30
+ */
31
+ headers: unknown | null;
32
+
33
+ /**
34
+ * An object containing details of your original forwarded request, for your ease
35
+ * of reference.
36
+ */
37
+ request: RequestForwardingForwardResponse.Request;
38
+
39
+ /**
40
+ * The HTTP status code of the forwarded request’s response, exactly received from
41
+ * the underlying integration’s API. This value will be returned as an integer.
42
+ */
43
+ statusCode: number;
44
+ }
45
+
46
+ export namespace RequestForwardingForwardResponse {
47
+ /**
48
+ * An object containing details of your original forwarded request, for your ease
49
+ * of reference.
50
+ */
51
+ export interface Request {
52
+ /**
53
+ * The body that was specified for the forwarded request. If a value was not
54
+ * specified in the original request, this value will be returned as null ;
55
+ * otherwise, this value will always be returned as a string.
56
+ */
57
+ data: string | null;
58
+
59
+ /**
60
+ * The specified HTTP headers that were included in the forwarded request. If no
61
+ * headers were specified, this will be returned as `null`.
62
+ */
63
+ headers: unknown | null;
64
+
65
+ /**
66
+ * The HTTP method that was specified for the forwarded request. Valid values
67
+ * include: `GET`, `POST`, `PUT` , `DELETE`, and `PATCH`.
68
+ */
69
+ method: string;
70
+
71
+ /**
72
+ * The query parameters that were included in the forwarded request. If no query
73
+ * parameters were specified, this will be returned as `null`.
74
+ */
75
+ params: unknown | null;
76
+
77
+ /**
78
+ * The URL route path that was specified for the forwarded request.
79
+ */
80
+ route: string;
81
+ }
82
+ }
83
+
84
+ export interface RequestForwardingForwardParams {
85
+ /**
86
+ * The HTTP method for the forwarded request. Valid values include: `GET`, `POST`,
87
+ * `PUT`, `DELETE`, and `PATCH`.
88
+ */
89
+ method: string;
90
+
91
+ /**
92
+ * The URL route path for the forwarded request. This value must begin with a
93
+ * forward-slash ( / ) and may only contain alphanumeric characters, hyphens, and
94
+ * underscores.
95
+ */
96
+ route: string;
97
+
98
+ /**
99
+ * The body for the forwarded request. This value must be specified as either a
100
+ * string or a valid JSON object.
101
+ */
102
+ data?: string | null;
103
+
104
+ /**
105
+ * The HTTP headers to include on the forwarded request. This value must be
106
+ * specified as an object of key-value pairs. Example:
107
+ * `{"Content-Type": "application/xml", "X-API-Version": "v1" }`
108
+ */
109
+ headers?: unknown | null;
110
+
111
+ /**
112
+ * The query parameters for the forwarded request. This value must be specified as
113
+ * a valid JSON object rather than a query string.
114
+ */
115
+ params?: unknown | null;
116
+ }
117
+
118
+ export namespace RequestForwarding {
119
+ export import RequestForwardingForwardResponse = API.RequestForwardingForwardResponse;
120
+ export import RequestForwardingForwardParams = API.RequestForwardingForwardParams;
121
+ }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = '5.2.0'; // x-release-please-version
1
+ export const VERSION = '5.4.0'; // x-release-please-version
package/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "5.2.0";
1
+ export declare const VERSION = "5.4.0";
2
2
  //# sourceMappingURL=version.d.ts.map
package/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
- exports.VERSION = '5.2.0'; // x-release-please-version
4
+ exports.VERSION = '5.4.0'; // x-release-please-version
5
5
  //# sourceMappingURL=version.js.map
package/version.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export const VERSION = '5.2.0'; // x-release-please-version
1
+ export const VERSION = '5.4.0'; // x-release-please-version
2
2
  //# sourceMappingURL=version.mjs.map