-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b3f60c
commit d2d3687
Showing
8 changed files
with
323 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Utils } from './Utils.js'; | ||
import { | ||
datasetAddRequestObject, | ||
datasetDeleteRequestObject, | ||
datasetGetRequestObject, | ||
datasetUpdateRequestObject | ||
} from './RequestObjects.js'; | ||
|
||
import { | ||
DatasetCreateUpdateOptions, | ||
DatasetDeleteOptions, | ||
DatasetGetOptions | ||
} from './interfaces/DatasetRequests.js'; | ||
|
||
/** | ||
* @description TODO | ||
*/ | ||
export class DatasetRequests { | ||
utils: Utils; | ||
|
||
constructor() { | ||
this.utils = new Utils(); | ||
} | ||
|
||
/** | ||
* @description Get the metadata, headers, and first items from the specified Dataset. | ||
* @todo Support queries | ||
* | ||
* @example | ||
* dataset.get(); | ||
*/ | ||
public async get(options: DatasetGetOptions) { | ||
const { datasetApiBaseUrl, datasetId } = options; | ||
|
||
return await this.utils.request(datasetGetRequestObject(datasetApiBaseUrl, datasetId)); | ||
} | ||
|
||
/** | ||
* @description Delete an item from the specified Dataset. | ||
* | ||
* @example | ||
* dataset.delete(); | ||
*/ | ||
public async delete(options: DatasetDeleteOptions) { | ||
const { datasetApiBaseUrl, datasetId, id } = options; | ||
|
||
await this.utils.request(datasetDeleteRequestObject(datasetApiBaseUrl, datasetId, 'item', id)); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @description Create an item in the Dataset. | ||
* | ||
* @example | ||
* dataset.create(input); | ||
*/ | ||
public async create(options: DatasetCreateUpdateOptions) { | ||
const { datasetApiBaseUrl, datasetId, id, input, properties } = options; | ||
|
||
const resourcePath = id ? `item/${id}` : `item`; | ||
|
||
const { success, errors } = this.utils.inputMatchesDatasetConfig(input, properties); | ||
if (!success) return { success, errors }; | ||
|
||
const payload = this.utils.inputToDatasetPayload(input, properties); | ||
|
||
await this.utils.request( | ||
datasetAddRequestObject(datasetApiBaseUrl, datasetId, resourcePath, payload) | ||
); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @description Update an item in the Dataset. | ||
* The deletion is based on the `itemId` set in the settings. | ||
* | ||
* @example | ||
* dataset.update(input); | ||
*/ | ||
public async update(options: DatasetCreateUpdateOptions) { | ||
const { datasetApiBaseUrl, datasetId, id, input, properties } = options; | ||
|
||
const { success, errors } = this.utils.inputMatchesDatasetConfig(input, properties); | ||
if (!success) return { success, errors }; | ||
|
||
const payload = this.utils.inputToDatasetPayload(input, properties); | ||
|
||
await this.utils.request( | ||
datasetUpdateRequestObject(datasetApiBaseUrl, datasetId, 'item', id, payload) | ||
); | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
//export * from './Compressor.js'; // TODO | ||
export * from './DatasetRequests.js'; | ||
export * from './ItemOptimizer.js'; | ||
export * from './RequestObjects.js'; | ||
export * from './Utils.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
type DatasetOptionsBase = { | ||
datasetApiBaseUrl: string; | ||
datasetId: string; | ||
}; | ||
|
||
export type DatasetGetOptions = DatasetOptionsBase; | ||
|
||
export type DatasetDeleteOptions = DatasetOptionsBase & { | ||
id: string; | ||
}; | ||
|
||
export type DatasetCreateUpdateOptions = DatasetOptionsBase & | ||
DatasetDeleteOptions & { | ||
input: Record<string, any>; | ||
properties: Record<string, any>[]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { test, expect } from 'vitest'; | ||
|
||
import { DatasetRequests } from '../src/DatasetRequests.js'; | ||
|
||
import { datasetGetResponse } from '../testdata/dataset.js'; | ||
|
||
const datasetApiBaseUrl = 'https://www.mockachino.com/6e5778ca-638a-4c'; | ||
const datasetId = 'asdf1234'; | ||
const id = 'abc123'; | ||
const input = { | ||
something: 'my value here' | ||
}; | ||
const properties = [ | ||
{ | ||
headerRef: 'abc123', | ||
headerType: 'short_text', | ||
value: '{input.something}', | ||
isRequired: true | ||
} | ||
]; | ||
|
||
/** | ||
* POSITIVE TESTS | ||
*/ | ||
test('It should make a Dataset create request', async () => { | ||
const expected = true; | ||
|
||
const result = await new DatasetRequests().create({ | ||
datasetApiBaseUrl, | ||
datasetId, | ||
id, | ||
input, | ||
properties | ||
}); | ||
|
||
expect(result).toBe(expected); | ||
}); | ||
|
||
test('It should make a Dataset create request for a specific ID', async () => { | ||
const expected = true; | ||
|
||
const result = await new DatasetRequests().create({ | ||
datasetApiBaseUrl, | ||
datasetId, | ||
id: '', | ||
input, | ||
properties | ||
}); | ||
|
||
expect(result).toBe(expected); | ||
}); | ||
|
||
test('It should make a Dataset update request', async () => { | ||
const expected = true; | ||
|
||
const result = await new DatasetRequests().update({ | ||
datasetApiBaseUrl, | ||
datasetId, | ||
id, | ||
input, | ||
properties | ||
}); | ||
|
||
expect(result).toBe(expected); | ||
}); | ||
|
||
test('It should make a Dataset delete request', async () => { | ||
const expected = true; | ||
|
||
const result = await new DatasetRequests().delete({ | ||
datasetApiBaseUrl, | ||
datasetId, | ||
id | ||
}); | ||
|
||
expect(result).toBe(expected); | ||
}); | ||
|
||
test('It should make a Dataset get request', async () => { | ||
const result = await new DatasetRequests().get({ | ||
datasetApiBaseUrl, | ||
datasetId | ||
}); | ||
|
||
expect(result).toMatchObject(datasetGetResponse); | ||
}); | ||
|
||
/** | ||
* NEGATIVE TESTS | ||
*/ | ||
test('It should not make a Dataset create request if the input does not match the configuration', async () => { | ||
const expected = { success: false, errors: ['Missing value for "{input.something}"'] }; | ||
|
||
const result = await new DatasetRequests().create({ | ||
datasetApiBaseUrl, | ||
datasetId, | ||
id, | ||
input: { x: 1 }, | ||
properties | ||
}); | ||
|
||
expect(result).toMatchObject(expected); | ||
}); | ||
|
||
test('It should not make a Dataset update request if the input does not match the configuration', async () => { | ||
const expected = { success: false, errors: ['Missing value for "{input.something}"'] }; | ||
|
||
const result = await new DatasetRequests().update({ | ||
datasetApiBaseUrl, | ||
datasetId, | ||
id, | ||
input: { x: 1 }, | ||
properties | ||
}); | ||
|
||
expect(result).toMatchObject(expected); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters