Skip to content

Commit 9314d96

Browse files
authored
Merge pull request #18 from alphamanuscript/add-full-update
Add full update on entities using put
2 parents 92ddd95 + 338bad6 commit 9314d96

File tree

8 files changed

+61
-150
lines changed

8 files changed

+61
-150
lines changed

packages/client/README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,23 @@ The entity above would be converted to:
7070
tags: []
7171
}
7272
```
73-
Dates are expanded as `text` for now and will correctly be expanded to `datetime` in future versions of `@surix/data-helpers`. Arrays are not supported for now.
73+
Dates created with the `Date()` function are expanded as `text`. and those created with `new Date()` are correctly expanded to `datetime`.
74+
75+
#### `project.entities.put(entity: Entity)`
76+
77+
Updates an entity by completely replacing its data.
78+
79+
```javascript
80+
const entity = {
81+
_id: 'someid', // The id of the entity to be updated
82+
data: {
83+
name: 'My name'
84+
}
85+
}
86+
87+
const changedEntity = await project.entities.put(entity);
88+
89+
```
7490
#### `project.entities.get(id: string)`
7591

7692
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
@@ -13,6 +13,16 @@ export function getProjectEntities (projectId: string, apiClient: AxiosInstance)
1313
const entities = res.data;
1414
return wrapEntityArray(entities);
1515
},
16+
async put(entity: Entity): Promise<WrappedEntity> {
17+
const _entity = { ...entity }; // Make a shallow copy
18+
const entityId = _entity._id;
19+
delete _entity._id;
20+
const expandedEntity = expandEntity(_entity);
21+
const res = await apiClient.put<ApiEntity>(
22+
`/projects/${projectId}/entities/${entityId}`, expandedEntity);
23+
const returnedEntity = res.data;
24+
return wrapEntity(returnedEntity);
25+
},
1626
async create(entity: Entity): Promise<WrappedEntity> {
1727
const expandedEntity = expandEntity(entity);
1828
const res = await apiClient.post<ApiEntity>(

packages/client/src/project/tests/__snapshots__/files.test.ts.snap

Lines changed: 0 additions & 132 deletions
This file was deleted.

packages/client/src/project/tests/__snapshots__/tags.test.ts.snap

Lines changed: 0 additions & 12 deletions
This file was deleted.

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,32 @@ describe('Project Entities', () => {
113113
expect(entity).toMatchSnapshot();
114114
});
115115
});
116+
describe('put', () => {
117+
const userEntity = {
118+
data: {
119+
name: 'someone 1'
120+
},
121+
tags: []
122+
}
123+
let apiClient: AxiosInstance;
124+
async function callMockPut (entity: any): Promise<dataHelpers.WrappedEntity> {
125+
apiClient = api.getApiClient('http://baseurl', 'somekey');
126+
jest.spyOn(apiClient, 'put').mockReturnValue(Promise.resolve({ data:
127+
{ entity } }));
128+
jest.spyOn(dataHelpers, 'wrapEntity');
129+
const project = getProjectApi('project1', apiClient);
130+
const ent = await project.entities.put(entity);
131+
return ent;
132+
}
133+
it('should call PUT /projects/:pid/entities/:eid endpoint with entity', async () => {
134+
const entity = { ...userEntity };
135+
entity['_id'] = 'entity1';
136+
const expectedEntity = dataHelpers.expandEntity(entity);
137+
const frozen = Object.freeze(entity);
138+
await callMockPut(frozen);
139+
expect(apiClient.put).toHaveBeenCalledWith(
140+
'/projects/project1/entities/entity1', expectedEntity);
141+
});
142+
});
116143
});
117144
});

packages/client/src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export interface Project {
99
export interface ProjectEntities {
1010
get(id: string): Promise<WrappedEntity>;
1111
query(query?: Query): Promise<WrappedEntity[]>;
12-
create(entity: any): Promise<WrappedEntity>;
12+
create(entity: Entity): Promise<WrappedEntity>;
13+
put(entity: Entity): Promise<WrappedEntity>;
1314
}
1415

1516
export interface ProjectFiles {
@@ -73,4 +74,5 @@ export interface DataField {
7374
export interface Entity {
7475
data: DataField;
7576
tags?: string[];
77+
_id?: string;
7678
}

0 commit comments

Comments
 (0)