feat: implement deleteListItemById method

This commit is contained in:
StevanFreeborn
2023-02-07 23:00:55 -06:00
parent 1b2c0e3e42
commit 48f62a80e9
2 changed files with 231 additions and 81 deletions
+9 -1
View File
@@ -267,7 +267,6 @@ export class OnspringClient {
); );
const apiResponse = await this.delete<any>(endpoint); const apiResponse = await this.delete<any>(endpoint);
return apiResponse; return apiResponse;
} }
@@ -292,6 +291,15 @@ export class OnspringClient {
return apiResponse.asListItemResponseType(); return apiResponse.asListItemResponseType();
} }
public async deleteListItemById(
listId: number,
itemId: string
): Promise<ApiResponse<any>> {
const endpoint = EndpointFactory.getDeleteListItemEndpoint(listId, itemId);
const apiResponse = await this.delete<any>(endpoint);
return apiResponse;
}
/** /**
* @method get - Makes a GET request to the specified endpoint. * @method get - Makes a GET request to the specified endpoint.
* @param {string} endpoint - The endpoint that will be used to make the request. * @param {string} endpoint - The endpoint that will be used to make the request.
+222 -80
View File
@@ -2203,106 +2203,248 @@ describe('OnspringClient', function () {
'3fa85f64-5717-4562-b3fc-2c963f66afa6' '3fa85f64-5717-4562-b3fc-2c963f66afa6'
); );
}); });
});
it('should return a promise that resolves to an api response when request receives a 401 response', async function () { it('should return a promise that resolves to an api response when request receives a 401 response', async function () {
const client = new OnspringClient(baseUrl, apiKey); const client = new OnspringClient(baseUrl, apiKey);
const mockAxiosClient = axios.create({ const mockAxiosClient = axios.create({
baseURL: baseUrl, baseURL: baseUrl,
headers: { headers: {
'x-apikey': apiKey, 'x-apikey': apiKey,
'x-api-version': '2', 'x-api-version': '2',
}, },
});
sinon.stub(mockAxiosClient, 'put').returns(
Promise.resolve({
status: 401,
statusText: 'Unauthorized',
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.addOrUpdateListItem(
new ListItemRequest(1, 'id', 'value', 1, 'color')
);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 401);
expect(result).to.have.property('isSuccessful', false);
expect(result.message).to.be.undefined;
expect(result.data).to.be.null;
}); });
sinon.stub(mockAxiosClient, 'put').returns( it('should return a promise that resolves to an api response when request receives a 403 response', async function () {
Promise.resolve({ const client = new OnspringClient(baseUrl, apiKey);
status: 401,
statusText: 'Unauthorized',
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient); const mockAxiosClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
});
const result = await client.addOrUpdateListItem( sinon.stub(mockAxiosClient, 'put').returns(
new ListItemRequest(1, 'id', 'value', 1, 'color') Promise.resolve({
); status: 403,
statusText: 'Forbidden',
data: { message: 'Forbidden' },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
expect(result).to.be.instanceOf(ApiResponse); sinon.stub(client, '_client' as any).value(mockAxiosClient);
expect(result).to.have.property('statusCode', 401);
expect(result).to.have.property('isSuccessful', false);
expect(result.message).to.be.undefined;
expect(result.data).to.be.null;
});
it('should return a promise that resolves to an api response when request receives a 403 response', async function () { const result = await client.addOrUpdateListItem(
const client = new OnspringClient(baseUrl, apiKey); new ListItemRequest(1, 'id', 'value', 1, 'color')
);
const mockAxiosClient = axios.create({ expect(result).to.be.instanceOf(ApiResponse);
baseURL: baseUrl, expect(result).to.have.property('statusCode', 403);
headers: { expect(result).to.have.property('isSuccessful', false);
'x-apikey': apiKey, expect(result).to.have.property('message', 'Forbidden');
'x-api-version': '2', expect(result.data).to.be.null;
},
}); });
sinon.stub(mockAxiosClient, 'put').returns( it('should return a promise that resolves to an api response when request receives a 404 response', async function () {
Promise.resolve({ const client = new OnspringClient(baseUrl, apiKey);
status: 403,
statusText: 'Forbidden',
data: { message: 'Forbidden' },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient); const mockAxiosClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
});
const result = await client.addOrUpdateListItem( sinon.stub(mockAxiosClient, 'put').returns(
new ListItemRequest(1, 'id', 'value', 1, 'color') Promise.resolve({
); status: 404,
statusText: 'Not Found',
data: { message: 'Not Found' },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
expect(result).to.be.instanceOf(ApiResponse); sinon.stub(client, '_client' as any).value(mockAxiosClient);
expect(result).to.have.property('statusCode', 403);
expect(result).to.have.property('isSuccessful', false); const result = await client.addOrUpdateListItem(
expect(result).to.have.property('message', 'Forbidden'); new ListItemRequest(1, 'id', 'value', 1, 'color')
expect(result.data).to.be.null; );
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 404);
expect(result).to.have.property('isSuccessful', false);
expect(result).to.have.property('message', 'Not Found');
expect(result.data).to.be.null;
});
}); });
it('should return a promise that resolves to an api response when request receives a 404 response', async function () { describe('deleteListItemById', function () {
const client = new OnspringClient(baseUrl, apiKey); it('should be defined', function () {
expect(OnspringClient.prototype.deleteListItemById).to.not.be.undefined;
const mockAxiosClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
}); });
sinon.stub(mockAxiosClient, 'put').returns( it('should be a function', function () {
Promise.resolve({ expect(OnspringClient.prototype.deleteListItemById).to.be.a('function');
status: 404, });
statusText: 'Not Found',
data: { message: 'Not Found' },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient); it('should return a promise', function () {
expect(
new OnspringClient(baseUrl, apiKey).deleteListItemById(1, 'id')
).to.be.instanceOf(Promise);
});
const result = await client.addOrUpdateListItem( it('should return a promise that resolves to an api response when request is successful', async function () {
new ListItemRequest(1, 'id', 'value', 1, 'color') const client = new OnspringClient(baseUrl, apiKey);
);
expect(result).to.be.instanceOf(ApiResponse); const mockAxiosClient = axios.create({
expect(result).to.have.property('statusCode', 404); baseURL: baseUrl,
expect(result).to.have.property('isSuccessful', false); headers: {
expect(result).to.have.property('message', 'Not Found'); 'x-apikey': apiKey,
expect(result.data).to.be.null; 'x-api-version': '2',
},
});
sinon.stub(mockAxiosClient, 'delete').returns(
Promise.resolve({
status: 204,
statusText: 'OK',
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.deleteListItemById(1, 'id');
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 204);
expect(result).to.have.property('isSuccessful', true);
expect(result).to.have.property('message', '');
expect(result).to.have.property('data', undefined);
});
it('should return a promise that resolves to an api response when request receives a 401 response', async function () {
const client = new OnspringClient(baseUrl, apiKey);
const mockAxiosClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
});
sinon.stub(mockAxiosClient, 'delete').returns(
Promise.resolve({
status: 401,
statusText: 'Unauthorized',
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.deleteListItemById(1, 'id');
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 401);
expect(result).to.have.property('isSuccessful', false);
expect(result.message).to.be.undefined;
expect(result.data).to.be.null;
});
it('should return a promise that resolves to an api response when request receives a 403 response', async function () {
const client = new OnspringClient(baseUrl, apiKey);
const mockAxiosClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
});
sinon.stub(mockAxiosClient, 'delete').returns(
Promise.resolve({
status: 403,
statusText: 'Forbidden',
data: { message: 'Forbidden' },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.deleteListItemById(1, 'id');
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 403);
expect(result).to.have.property('isSuccessful', false);
expect(result).to.have.property('message', 'Forbidden');
expect(result.data).to.be.null;
});
it('should return a promise that resolves to an api response when request receives a 404 response', async function () {
const client = new OnspringClient(baseUrl, apiKey);
const mockAxiosClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
});
sinon.stub(mockAxiosClient, 'delete').returns(
Promise.resolve({
status: 404,
statusText: 'Not Found',
data: { message: 'Not Found' },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.deleteListItemById(1, 'id');
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 404);
expect(result).to.have.property('isSuccessful', false);
expect(result).to.have.property('message', 'Not Found');
expect(result.data).to.be.null;
});
}); });
}); });