Skip to content

Commit 1def35c

Browse files
author
grafitto
committed
Solved merge conflicts
2 parents cf3b6f1 + 9314d96 commit 1def35c

File tree

6 files changed

+65
-10
lines changed

6 files changed

+65
-10
lines changed

packages/client/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ const entity = {
8989
const updatedEntity = await project.entities.patch(entity);
9090
```
9191

92+
Dates created with the `Date()` function are expanded as `text`. and those created with `new Date()` are correctly expanded to `datetime`.
93+
94+
#### `project.entities.put(entity: Entity)`
95+
96+
Updates an entity by completely replacing its data.
97+
98+
```javascript
99+
const entity = {
100+
_id: 'someid', // The id of the entity to be updated
101+
data: {
102+
name: 'My name'
103+
}
104+
}
105+
106+
const changedEntity = await project.entities.put(entity);
107+
108+
```
92109
#### `project.entities.get(id: string)`
93110

94111
Fetches an entity by ID.

packages/client/src/api/api.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import axios, { AxiosInstance } from 'axios';
33
export function getApiClient (baseUrl: string, apiKey: string): AxiosInstance {
44
const axiosInstance = axios.create({
55
baseURL: baseUrl
6-
})
7-
axiosInstance.defaults.headers['Authorization'] = `apikey ${apiKey}`
8-
return axiosInstance
6+
});
7+
axiosInstance.defaults.headers['Authorization'] = `apikey ${apiKey}`;
8+
return axiosInstance;
99
}

packages/client/src/client/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class Client {
2323
}, opts);
2424
let baseUrl = _opts.environment === 'production' ? PRODUCTION_URL : STAGING_URL;
2525
baseUrl = _opts.baseUrl || baseUrl;
26-
const apiKey = `${_opts.keyId}:${_opts.keySecret}`
26+
const apiKey = `${_opts.keyId}:${_opts.keySecret}`;
2727
this._apiClient = getApiClient(baseUrl, apiKey);
2828
}
2929

packages/client/src/project/entities.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ export function getProjectEntities (projectId: string, apiClient: AxiosInstance)
2323
const returnedEntity = res.data;
2424
return wrapEntity(returnedEntity);
2525
},
26+
async put(entity: Entity): Promise<WrappedEntity> {
27+
const _entity = { ...entity }; // Make a shallow copy
28+
const entityId = _entity._id;
29+
delete _entity._id;
30+
const expandedEntity = expandEntity(_entity);
31+
const res = await apiClient.put<ApiEntity>(
32+
`/projects/${projectId}/entities/${entityId}`, expandedEntity);
33+
const returnedEntity = res.data;
34+
return wrapEntity(returnedEntity);
35+
},
2636
async create(entity: Entity): Promise<WrappedEntity> {
2737
const expandedEntity = expandEntity(entity);
2838
const res = await apiClient.post<ApiEntity>(

packages/client/src/project/tests/entities.test.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,40 @@ describe('Project Entities', () => {
131131
}
132132
it('should call PATCH /projects/:pid/entities/:eid endpoint with entity', async () => {
133133
const entity = { ...userEntity };
134-
entity['_id'] = 'entity1';
135-
const expectedEntity = dataHelpers.expandEntity(entity);
136-
const frozen = Object.freeze(entity);
137-
await callMockUpdate(frozen);
138-
expect(apiClient.patch).toHaveBeenCalledWith(
139-
'/projects/project1/entities/entity1', expectedEntity);
134+
entity['_id'] = 'entity1';
135+
const expectedEntity = dataHelpers.expandEntity(entity);
136+
const frozen = Object.freeze(entity);
137+
await callMockUpdate(frozen);
138+
expect(apiClient.patch).toHaveBeenCalledWith(
139+
'/projects/project1/entities/entity1', expectedEntity);
140140
});
141141
});
142+
describe('put', () => {
143+
const userEntity = {
144+
data: {
145+
name: 'someone 1'
146+
},
147+
tags: []
148+
};
149+
let apiClient: AxiosInstance;
150+
async function callMockPut (entity: any): Promise<dataHelpers.WrappedEntity> {
151+
apiClient = api.getApiClient('http://baseurl', 'somekey');
152+
jest.spyOn(apiClient, 'put').mockReturnValue(Promise.resolve({ data:
153+
{ entity } }));
154+
jest.spyOn(dataHelpers, 'wrapEntity');
155+
const project = getProjectApi('project1', apiClient);
156+
const ent = await project.entities.put(entity);
157+
return ent;
158+
}
159+
it('should call PUT /projects/:pid/entities/:eid endpoint with entity', async () => {
160+
const entity = { ...userEntity };
161+
entity['_id'] = 'entity1';
162+
const expectedEntity = dataHelpers.expandEntity(entity);
163+
const frozen = Object.freeze(entity);
164+
await callMockPut(frozen);
165+
expect(apiClient.put).toHaveBeenCalledWith(
166+
'/projects/project1/entities/entity1', expectedEntity);
167+
});
168+
});
142169
});
143170
});

packages/client/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface ProjectEntities {
1111
query(query?: Query): Promise<WrappedEntity[]>;
1212
create(entity: Entity): Promise<WrappedEntity>;
1313
update(entity: Entity): Promise<WrappedEntity>;
14+
put(entity: Entity): Promise<WrappedEntity>;
1415
}
1516

1617
export interface ProjectFiles {

0 commit comments

Comments
 (0)