Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,22 @@ export class LandingAIADE {
* `https://api.va.eu-west-1.landing.ai/v1/ade/classify`.
*/
classify(
body: TopLevelAPI.ClassifyParams,
body: TopLevelAPI.ClassifyParams & { saveTo?: string },
options?: RequestOptions,
): APIPromise<TopLevelAPI.ClassifyResponse> {
return this.post('/v1/ade/classify', multipartFormRequestOptions({ body, ...options }, this));
const { saveTo, ...apiBody } = body;
const promise = this.post<TopLevelAPI.ClassifyResponse>(
'/v1/ade/classify',
multipartFormRequestOptions({ body: apiBody, ...options }, this),
);
if (saveTo) {
const filename = _getInputFilename(apiBody.document, apiBody.document_url);
return promise._thenUnwrap((data) => {
_saveResponse(saveTo, filename, 'classify', data);
return data;
});
}
return promise;
}

/**
Expand Down Expand Up @@ -417,10 +429,22 @@ export class LandingAIADE {
* `https://api.va.eu-west-1.landing.ai/v1/ade/section`.
*/
section(
body: TopLevelAPI.SectionParams,
body: TopLevelAPI.SectionParams & { saveTo?: string },
options?: RequestOptions,
): APIPromise<TopLevelAPI.SectionResponse> {
return this.post('/v1/ade/section', multipartFormRequestOptions({ body, ...options }, this));
const { saveTo, ...apiBody } = body;
const promise = this.post<TopLevelAPI.SectionResponse>(
'/v1/ade/section',
multipartFormRequestOptions({ body: apiBody, ...options }, this),
);
if (saveTo) {
const filename = _getInputFilename(apiBody.markdown, apiBody.markdown_url);
return promise._thenUnwrap((data) => {
_saveResponse(saveTo, filename, 'section', data);
return data;
});
}
return promise;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion tests/save-to.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ describe('_saveResponse', () => {

_saveResponse(testDir, 'myinput', 'split', result);
expect(fs.existsSync(path.join(testDir, 'myinput_split_output.json'))).toBe(true);

_saveResponse(testDir, 'myinput', 'classify', result);
expect(fs.existsSync(path.join(testDir, 'myinput_classify_output.json'))).toBe(true);

_saveResponse(testDir, 'myinput', 'section', result);
expect(fs.existsSync(path.join(testDir, 'myinput_section_output.json'))).toBe(true);
});

it('creates nested directories', () => {
Expand All @@ -128,7 +134,7 @@ describe('_saveResponse', () => {

it('skips redundant "output" prefix when filename is "output"', () => {
const result = {};
for (const method of ['parse', 'extract', 'split']) {
for (const method of ['parse', 'extract', 'split', 'classify', 'section']) {
_saveResponse(testDir, 'output', method, result);
expect(fs.existsSync(path.join(testDir, `${method}_output.json`))).toBe(true);
expect(fs.existsSync(path.join(testDir, `output_${method}_output.json`))).toBe(false);
Expand Down
Loading