Skip to content

Commit

Permalink
Merge pull request #238 from skedify/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarrku committed Apr 14, 2021
2 parents 5e6cc99 + 89355e3 commit 9a139c8
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
22 changes: 22 additions & 0 deletions src/Skedify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand Down
23 changes: 23 additions & 0 deletions src/__snapshots__/Skedify.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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`] = `
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
29 changes: 29 additions & 0 deletions src/decorators/withResources/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from './invariants'
import {
HTTP_VERB_PATCH,
HTTP_VERB_PUT,
HTTP_VERB_POST,
HTTP_VERB_DELETE,
HTTP_VERB_ALL_WILDCARD,
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 9a139c8

Please sign in to comment.