diff --git a/README.md b/README.md index 8d6459f..0a14723 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ SDK.subjects() // the name of the collection is available as a function on the S // In addition to the attributes from the response data, the record will also have additional methods record.save(); // to save (only) the made changes to the API. Corresponds to a PATCH request. record.delete(); // to delete this one member from the collection. Corresponds to a DELETE request. - record.replace(); // to replace all data from this member with another but reusing the ID. Corresponds to a PUT request.11 + record.replace(); // to replace all data from this member with another but reusing the ID. Corresponds to a PUT request. }); }, error => { diff --git a/src/Skedify.test.js b/src/Skedify.test.js index d80115f..f110d8e 100644 --- a/src/Skedify.test.js +++ b/src/Skedify.test.js @@ -954,6 +954,14 @@ describe('API', () => { ).toThrowErrorMatchingSnapshot() }) + it('should be possible to disable certain actions (replace) on a resource', () => { + expect(() => + SDK.insights('cumulio').auth().replace({ + id: 'new id', + }) + ).toThrowErrorMatchingSnapshot() + }) + it('should be possible to disable certain actions (delete) on a resource', () => { expect(() => SDK.insights('cumulio').auth().delete() @@ -1197,6 +1205,20 @@ describe('API', () => { ).toMatchSnapshot() }) + it('should be possible to replace an entity', async () => { + expect( + await matchRequest( + SDK.appointments(1207) + .replace({ + state: 'cancelled', + cancelled_by_type: 'customer', + cancelled_by_id: 'customer id goes here', + }) + .then((appointment) => appointment.save()) + ) + ).toMatchSnapshot() + }) + it('should be possible to delete an entity', async () => { expect( await matchRequest( diff --git a/src/__snapshots__/Skedify.test.js.snap b/src/__snapshots__/Skedify.test.js.snap index 4f9c0d2..c06309c 100644 --- a/src/__snapshots__/Skedify.test.js.snap +++ b/src/__snapshots__/Skedify.test.js.snap @@ -177,6 +177,8 @@ exports[`API API/Resources should be possible to disable certain actions (new) b exports[`API API/Resources should be possible to disable certain actions (new) on a resource 1`] = `"[CONFIG]: You tried to call \`.new({\\"id\\":\\"new identity id\\"})\` but this method is currently not allowed."`; +exports[`API API/Resources should be possible to disable certain actions (replace) on a resource 1`] = `"[CONFIG]: You tried to call \`.replace({\\"id\\":\\"new id\\"})\` but this method is currently not allowed."`; + exports[`API API/Resources should be possible to disable certain actions (update) on a resource 1`] = `"[CONFIG]: You tried to call \`.update({\\"id\\":\\"new id\\"})\` but this method is currently not allowed."`; exports[`API API/Resources should be possible to make subsequent calls to include to add multiple includes 1`] = ` @@ -296,6 +298,27 @@ Object { } `; +exports[`API API/Resources should be possible to replace an entity 1`] = ` +Object { + "data": Object { + "cancelled_by_id": "customer id goes here", + "cancelled_by_type": "customer", + "state": "cancelled", + }, + "headers": Object { + "Accept": "application/json, text/plain, */*", + "Accept-Language": "nl-BE, nl;q=0.667, *;q=0.333", + "Authorization": "Bearer fake_example_access_token", + "Cache-Control": "no-store", + "Content-Type": "application/json;charset=utf-8", + "Pragma": "no-cache", + }, + "method": "put", + "params": undefined, + "url": "https://api.example.com/appointments/1207", +} +`; + exports[`API API/Resources should be possible to request a single subject 1`] = ` Object { "data": undefined, diff --git a/src/constants/index.js b/src/constants/index.js index 5054f2f..ec91a3d 100644 --- a/src/constants/index.js +++ b/src/constants/index.js @@ -2,7 +2,8 @@ export * from './exported' export const HTTP_VERB_ALL_WILDCARD = '*' +export const HTTP_VERB_DELETE = 'delete' export const HTTP_VERB_GET = 'get' -export const HTTP_VERB_POST = 'post' export const HTTP_VERB_PATCH = 'patch' -export const HTTP_VERB_DELETE = 'delete' +export const HTTP_VERB_POST = 'post' +export const HTTP_VERB_PUT = 'put' diff --git a/src/decorators/withResources/Resource.js b/src/decorators/withResources/Resource.js index e67d602..3e17930 100644 --- a/src/decorators/withResources/Resource.js +++ b/src/decorators/withResources/Resource.js @@ -8,6 +8,7 @@ import { } from './invariants' import { HTTP_VERB_PATCH, + HTTP_VERB_PUT, HTTP_VERB_POST, HTTP_VERB_DELETE, HTTP_VERB_ALL_WILDCARD, @@ -226,6 +227,34 @@ export default class Resource { return cloned } + replace(data) { + if (!isAllowed(this, HTTP_VERB_PUT)) { + throw createConfigError( + `You tried to call \`.replace(${JSON.stringify( + data + )})\` but this method is currently not allowed.` + ) + } + + // Clone the current Resource + const cloned = overrideThen(clone(this), (originalThen) => ({ + save() { + return new Promise(originalThen) + }, + })) + + // Override request config + set(cloned, ({ requestConfig }) => ({ + requestConfig: Object.assign({}, requestConfig, { + data, + method: HTTP_VERB_PUT, + name: 'put', + }), + })) + + return cloned + } + delete() { if (!isAllowed(this, HTTP_VERB_DELETE)) { throw createConfigError(