@tryfinch/finch-api 3.0.0 → 3.1.1
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.
- package/CHANGELOG.md +181 -0
- package/LICENSE +201 -0
- package/README.md +225 -0
- package/_shims/formdata.node.d.ts +1 -0
- package/core.d.ts +9 -1
- package/core.d.ts.map +1 -1
- package/core.js +61 -18
- package/core.js.map +1 -1
- package/core.mjs +59 -18
- package/core.mjs.map +1 -1
- package/error.d.ts +4 -0
- package/error.d.ts.map +1 -1
- package/error.js +8 -0
- package/error.js.map +1 -1
- package/error.mjs +6 -0
- package/error.mjs.map +1 -1
- package/index.d.mts +10 -0
- package/index.d.ts +10 -0
- package/index.d.ts.map +1 -1
- package/index.js +7 -0
- package/index.js.map +1 -1
- package/index.mjs +6 -0
- package/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/_shims/fileFromPath.node.ts +1 -1
- package/src/_shims/fileFromPath.ts +1 -1
- package/src/_shims/formdata.node.d.ts +1 -0
- package/src/_shims/getMultipartRequestOptions.node.ts +1 -1
- package/src/_shims/getMultipartRequestOptions.ts +1 -1
- package/src/core.ts +63 -18
- package/src/error.ts +8 -0
- package/src/index.ts +14 -1
- package/src/resources/account.ts +3 -3
- package/src/resources/ats/applications.ts +6 -6
- package/src/resources/ats/ats.ts +2 -2
- package/src/resources/ats/candidates.ts +5 -5
- package/src/resources/ats/jobs.ts +5 -5
- package/src/resources/ats/offers.ts +5 -5
- package/src/resources/ats/stages.ts +4 -4
- package/src/resources/hris/benefits/benefits.ts +5 -5
- package/src/resources/hris/benefits/individuals.ts +6 -6
- package/src/resources/hris/company.ts +4 -4
- package/src/resources/hris/directory.ts +5 -5
- package/src/resources/hris/hris.ts +2 -2
- package/src/resources/hris/individuals/employment-data.ts +5 -5
- package/src/resources/hris/individuals/individuals.ts +6 -6
- package/src/resources/hris/pay-statements.ts +6 -6
- package/src/resources/hris/payments.ts +5 -5
- package/src/resources/providers.ts +4 -4
- package/src/uploads.ts +11 -10
- package/src/version.ts +1 -1
- package/uploads.d.ts +2 -2
- package/uploads.d.ts.map +1 -1
- package/uploads.js +3 -2
- package/uploads.js.map +1 -1
- package/uploads.mjs +3 -2
- package/uploads.mjs.map +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
- package/src/streaming.ts +0 -204
- package/streaming.d.ts +0 -14
- package/streaming.d.ts.map +0 -1
- package/streaming.js +0 -155
- package/streaming.js.map +0 -1
- package/streaming.mjs +0 -151
- package/streaming.mjs.map +0 -1
package/streaming.js
DELETED
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
-
exports.Stream = void 0;
|
|
4
|
-
const core_1 = require('./core.js');
|
|
5
|
-
class SSEDecoder {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.event = null;
|
|
8
|
-
this.data = [];
|
|
9
|
-
this.chunks = [];
|
|
10
|
-
}
|
|
11
|
-
decode(line) {
|
|
12
|
-
if (line.endsWith('\r')) {
|
|
13
|
-
line = line.substring(0, line.length - 1);
|
|
14
|
-
}
|
|
15
|
-
if (!line) {
|
|
16
|
-
// empty line and we didn't previously encounter any messages
|
|
17
|
-
if (!this.event && !this.data.length) return null;
|
|
18
|
-
const sse = {
|
|
19
|
-
event: this.event,
|
|
20
|
-
data: this.data.join('\n'),
|
|
21
|
-
raw: this.chunks,
|
|
22
|
-
};
|
|
23
|
-
this.event = null;
|
|
24
|
-
this.data = [];
|
|
25
|
-
this.chunks = [];
|
|
26
|
-
return sse;
|
|
27
|
-
}
|
|
28
|
-
this.chunks.push(line);
|
|
29
|
-
if (line.startsWith(':')) {
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
let [fieldname, _, value] = partition(line, ':');
|
|
33
|
-
if (value.startsWith(' ')) {
|
|
34
|
-
value = value.substring(1);
|
|
35
|
-
}
|
|
36
|
-
if (fieldname === 'event') {
|
|
37
|
-
this.event = value;
|
|
38
|
-
} else if (fieldname === 'data') {
|
|
39
|
-
this.data.push(value);
|
|
40
|
-
}
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
class Stream {
|
|
45
|
-
constructor(response, controller) {
|
|
46
|
-
this.response = response;
|
|
47
|
-
this.controller = controller;
|
|
48
|
-
this.decoder = new SSEDecoder();
|
|
49
|
-
this.responseHeaders = (0, core_1.createResponseHeaders)(response.headers);
|
|
50
|
-
}
|
|
51
|
-
async *iterMessages() {
|
|
52
|
-
if (!this.response.body) {
|
|
53
|
-
this.controller.abort();
|
|
54
|
-
throw new Error(`Attempted to iterate over a response with no body`);
|
|
55
|
-
}
|
|
56
|
-
const lineDecoder = new LineDecoder();
|
|
57
|
-
// @ts-ignore
|
|
58
|
-
for await (const chunk of this.response.body) {
|
|
59
|
-
let text;
|
|
60
|
-
if (chunk instanceof Buffer) {
|
|
61
|
-
text = chunk.toString();
|
|
62
|
-
} else if (chunk instanceof Uint8Array) {
|
|
63
|
-
text = Buffer.from(chunk).toString();
|
|
64
|
-
} else {
|
|
65
|
-
text = chunk;
|
|
66
|
-
}
|
|
67
|
-
for (const line of lineDecoder.decode(text)) {
|
|
68
|
-
const sse = this.decoder.decode(line);
|
|
69
|
-
if (sse) yield sse;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
for (const line of lineDecoder.flush()) {
|
|
73
|
-
const sse = this.decoder.decode(line);
|
|
74
|
-
if (sse) yield sse;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
async *[Symbol.asyncIterator]() {
|
|
78
|
-
try {
|
|
79
|
-
for await (const sse of this.iterMessages()) {
|
|
80
|
-
try {
|
|
81
|
-
yield JSON.parse(sse.data);
|
|
82
|
-
} catch (e) {
|
|
83
|
-
console.error(`Could not parse message into JSON:`, sse.data);
|
|
84
|
-
console.error(`From chunk:`, sse.raw);
|
|
85
|
-
throw e;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
} catch (e) {
|
|
89
|
-
// If the user calls `stream.controller.abort()`, we should exit without throwing.
|
|
90
|
-
if (e instanceof Error && e.name === 'AbortError') return;
|
|
91
|
-
throw e;
|
|
92
|
-
} finally {
|
|
93
|
-
// If the user `break`s, abort the ongoing request.
|
|
94
|
-
this.controller.abort();
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
exports.Stream = Stream;
|
|
99
|
-
const NEWLINE_CHARS = '\n\r\x0b\x0c\x1c\x1d\x1e\x85\u2028\u2029';
|
|
100
|
-
/**
|
|
101
|
-
* A re-implementation of httpx's `LineDecoder` in Python that handles incrementally
|
|
102
|
-
* reading lines from text.
|
|
103
|
-
*
|
|
104
|
-
* https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258
|
|
105
|
-
*/
|
|
106
|
-
class LineDecoder {
|
|
107
|
-
constructor() {
|
|
108
|
-
this.buffer = [];
|
|
109
|
-
this.trailingCR = false;
|
|
110
|
-
}
|
|
111
|
-
decode(text) {
|
|
112
|
-
if (this.trailingCR) {
|
|
113
|
-
text = '\r' + text;
|
|
114
|
-
this.trailingCR = false;
|
|
115
|
-
}
|
|
116
|
-
if (text.endsWith('\r')) {
|
|
117
|
-
this.trailingCR = true;
|
|
118
|
-
text = text.slice(0, -1);
|
|
119
|
-
}
|
|
120
|
-
if (!text) {
|
|
121
|
-
return [];
|
|
122
|
-
}
|
|
123
|
-
const trailing_newline = NEWLINE_CHARS.includes(text.slice(-1));
|
|
124
|
-
let lines = text.split(/\r\n|[\n\r\x0b\x0c\x1c\x1d\x1e\x85\u2028\u2029]/g);
|
|
125
|
-
if (lines.length === 1 && !trailing_newline) {
|
|
126
|
-
this.buffer.push(lines[0]);
|
|
127
|
-
return [];
|
|
128
|
-
}
|
|
129
|
-
if (this.buffer.length > 0) {
|
|
130
|
-
lines = [this.buffer.join('') + lines[0], ...lines.slice(1)];
|
|
131
|
-
this.buffer = [];
|
|
132
|
-
}
|
|
133
|
-
if (!trailing_newline) {
|
|
134
|
-
this.buffer = [lines.pop() || ''];
|
|
135
|
-
}
|
|
136
|
-
return lines;
|
|
137
|
-
}
|
|
138
|
-
flush() {
|
|
139
|
-
if (!this.buffer.length && !this.trailingCR) {
|
|
140
|
-
return [];
|
|
141
|
-
}
|
|
142
|
-
const lines = [this.buffer.join('')];
|
|
143
|
-
this.buffer = [];
|
|
144
|
-
this.trailingCR = false;
|
|
145
|
-
return lines;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
function partition(str, delimiter) {
|
|
149
|
-
const index = str.indexOf(delimiter);
|
|
150
|
-
if (index !== -1) {
|
|
151
|
-
return [str.substring(0, index), delimiter, str.substring(index + delimiter.length)];
|
|
152
|
-
}
|
|
153
|
-
return [str, '', ''];
|
|
154
|
-
}
|
|
155
|
-
//# sourceMappingURL=streaming.js.map
|
package/streaming.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"streaming.js","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":";;;AACA,oCAAqE;AAQrE,MAAM,UAAU;IAKd;QACE,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;IACnB,CAAC;IAED,MAAM,CAAC,IAAY;QACjB,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE;YACvB,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;SAC3C;QAED,IAAI,CAAC,IAAI,EAAE;YACT,6DAA6D;YAC7D,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM;gBAAE,OAAO,IAAI,CAAC;YAElD,MAAM,GAAG,GAAoB;gBAC3B,KAAK,EAAE,I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C;AACvB,CAAC"}
|
package/streaming.mjs
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import { createResponseHeaders } from './core.mjs';
|
|
2
|
-
class SSEDecoder {
|
|
3
|
-
constructor() {
|
|
4
|
-
this.event = null;
|
|
5
|
-
this.data = [];
|
|
6
|
-
this.chunks = [];
|
|
7
|
-
}
|
|
8
|
-
decode(line) {
|
|
9
|
-
if (line.endsWith('\r')) {
|
|
10
|
-
line = line.substring(0, line.length - 1);
|
|
11
|
-
}
|
|
12
|
-
if (!line) {
|
|
13
|
-
// empty line and we didn't previously encounter any messages
|
|
14
|
-
if (!this.event && !this.data.length) return null;
|
|
15
|
-
const sse = {
|
|
16
|
-
event: this.event,
|
|
17
|
-
data: this.data.join('\n'),
|
|
18
|
-
raw: this.chunks,
|
|
19
|
-
};
|
|
20
|
-
this.event = null;
|
|
21
|
-
this.data = [];
|
|
22
|
-
this.chunks = [];
|
|
23
|
-
return sse;
|
|
24
|
-
}
|
|
25
|
-
this.chunks.push(line);
|
|
26
|
-
if (line.startsWith(':')) {
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
let [fieldname, _, value] = partition(line, ':');
|
|
30
|
-
if (value.startsWith(' ')) {
|
|
31
|
-
value = value.substring(1);
|
|
32
|
-
}
|
|
33
|
-
if (fieldname === 'event') {
|
|
34
|
-
this.event = value;
|
|
35
|
-
} else if (fieldname === 'data') {
|
|
36
|
-
this.data.push(value);
|
|
37
|
-
}
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
export class Stream {
|
|
42
|
-
constructor(response, controller) {
|
|
43
|
-
this.response = response;
|
|
44
|
-
this.controller = controller;
|
|
45
|
-
this.decoder = new SSEDecoder();
|
|
46
|
-
this.responseHeaders = createResponseHeaders(response.headers);
|
|
47
|
-
}
|
|
48
|
-
async *iterMessages() {
|
|
49
|
-
if (!this.response.body) {
|
|
50
|
-
this.controller.abort();
|
|
51
|
-
throw new Error(`Attempted to iterate over a response with no body`);
|
|
52
|
-
}
|
|
53
|
-
const lineDecoder = new LineDecoder();
|
|
54
|
-
// @ts-ignore
|
|
55
|
-
for await (const chunk of this.response.body) {
|
|
56
|
-
let text;
|
|
57
|
-
if (chunk instanceof Buffer) {
|
|
58
|
-
text = chunk.toString();
|
|
59
|
-
} else if (chunk instanceof Uint8Array) {
|
|
60
|
-
text = Buffer.from(chunk).toString();
|
|
61
|
-
} else {
|
|
62
|
-
text = chunk;
|
|
63
|
-
}
|
|
64
|
-
for (const line of lineDecoder.decode(text)) {
|
|
65
|
-
const sse = this.decoder.decode(line);
|
|
66
|
-
if (sse) yield sse;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
for (const line of lineDecoder.flush()) {
|
|
70
|
-
const sse = this.decoder.decode(line);
|
|
71
|
-
if (sse) yield sse;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
async *[Symbol.asyncIterator]() {
|
|
75
|
-
try {
|
|
76
|
-
for await (const sse of this.iterMessages()) {
|
|
77
|
-
try {
|
|
78
|
-
yield JSON.parse(sse.data);
|
|
79
|
-
} catch (e) {
|
|
80
|
-
console.error(`Could not parse message into JSON:`, sse.data);
|
|
81
|
-
console.error(`From chunk:`, sse.raw);
|
|
82
|
-
throw e;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
} catch (e) {
|
|
86
|
-
// If the user calls `stream.controller.abort()`, we should exit without throwing.
|
|
87
|
-
if (e instanceof Error && e.name === 'AbortError') return;
|
|
88
|
-
throw e;
|
|
89
|
-
} finally {
|
|
90
|
-
// If the user `break`s, abort the ongoing request.
|
|
91
|
-
this.controller.abort();
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
const NEWLINE_CHARS = '\n\r\x0b\x0c\x1c\x1d\x1e\x85\u2028\u2029';
|
|
96
|
-
/**
|
|
97
|
-
* A re-implementation of httpx's `LineDecoder` in Python that handles incrementally
|
|
98
|
-
* reading lines from text.
|
|
99
|
-
*
|
|
100
|
-
* https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258
|
|
101
|
-
*/
|
|
102
|
-
class LineDecoder {
|
|
103
|
-
constructor() {
|
|
104
|
-
this.buffer = [];
|
|
105
|
-
this.trailingCR = false;
|
|
106
|
-
}
|
|
107
|
-
decode(text) {
|
|
108
|
-
if (this.trailingCR) {
|
|
109
|
-
text = '\r' + text;
|
|
110
|
-
this.trailingCR = false;
|
|
111
|
-
}
|
|
112
|
-
if (text.endsWith('\r')) {
|
|
113
|
-
this.trailingCR = true;
|
|
114
|
-
text = text.slice(0, -1);
|
|
115
|
-
}
|
|
116
|
-
if (!text) {
|
|
117
|
-
return [];
|
|
118
|
-
}
|
|
119
|
-
const trailing_newline = NEWLINE_CHARS.includes(text.slice(-1));
|
|
120
|
-
let lines = text.split(/\r\n|[\n\r\x0b\x0c\x1c\x1d\x1e\x85\u2028\u2029]/g);
|
|
121
|
-
if (lines.length === 1 && !trailing_newline) {
|
|
122
|
-
this.buffer.push(lines[0]);
|
|
123
|
-
return [];
|
|
124
|
-
}
|
|
125
|
-
if (this.buffer.length > 0) {
|
|
126
|
-
lines = [this.buffer.join('') + lines[0], ...lines.slice(1)];
|
|
127
|
-
this.buffer = [];
|
|
128
|
-
}
|
|
129
|
-
if (!trailing_newline) {
|
|
130
|
-
this.buffer = [lines.pop() || ''];
|
|
131
|
-
}
|
|
132
|
-
return lines;
|
|
133
|
-
}
|
|
134
|
-
flush() {
|
|
135
|
-
if (!this.buffer.length && !this.trailingCR) {
|
|
136
|
-
return [];
|
|
137
|
-
}
|
|
138
|
-
const lines = [this.buffer.join('')];
|
|
139
|
-
this.buffer = [];
|
|
140
|
-
this.trailingCR = false;
|
|
141
|
-
return lines;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
function partition(str, delimiter) {
|
|
145
|
-
const index = str.indexOf(delimiter);
|
|
146
|
-
if (index !== -1) {
|
|
147
|
-
return [str.substring(0, index), delimiter, str.substring(index + delimiter.length)];
|
|
148
|
-
}
|
|
149
|
-
return [str, '', ''];
|
|
150
|
-
}
|
|
151
|
-
//# sourceMappingURL=streaming.mjs.map
|
package/streaming.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"streaming.mjs","sourceRoot":"","sources":["src/streaming.ts"],"names":[],"mappings":"OACO,EAAwB,qBAAqB,EAAE;AAQtD,MAAM,UAAU;IAKd;QACE,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;IACnB,CAAC;IAED,MAAM,CAAC,IAAY;QACjB,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE;YACvB,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;SAC3C;QAED,IAAI,CAAC,IAAI,EAAE;YACT,6DAA6D;YAC7D,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM;gBAAE,OAAO,IAAI,CAAC;YAElD,MAAM,GAAG,GAAoB;gBAC3B,KAAK,EAAE,IAAI,CAAC,KAAK;gBACjB,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;gBAC1B,GAAG,EAAE,IAAI,CAAC,MAAM;aACjB,CAAC;YAEF,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;YAClB,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC;YACf,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;YAEjB,OAAO,GAAG,CAAC;SACZ;QAED,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAEvB,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;YACxB,OAAO,IAAI,C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