Skip to content

Commit fe51716

Browse files
authored
Merge pull request #21 from alphamanuscript/add-delete
Adds delete and deleteMany on client
2 parents 1cfae1a + 23ae409 commit fe51716

File tree

7 files changed

+256
-4
lines changed

7 files changed

+256
-4
lines changed

packages/client/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,28 @@ entities.forEach((entity) => {
144144
});
145145
```
146146

147+
#### `project.entities.delete(id: string)`
148+
149+
Deletes an entity entirely.
150+
151+
```javascript
152+
const entityId = 'someid';
153+
const deletedEntity = await project.entities.delete(entityId);
154+
155+
console.log(deletedEntity.get('name') + ' was deleted');
156+
```
157+
158+
#### `project.entities.deleteMany(ids: EntityIds)`
159+
160+
Deletes a bunch of entities identified by the provided ids.
161+
162+
```javascript
163+
const entitiesIds = ['someid1', 'someid2'];
164+
165+
const response = await project.entities.deleteMany(entitiesIds);
166+
console.log(response.deleted); // Number of deleted entities
167+
```
168+
147169
### Files
148170

149171
#### `project.files.get(id: string)`

packages/client/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@surix/client",
3-
"version": "0.7.2",
3+
"version": "0.8.0",
44
"description": "Surix API Client",
55
"keywords": [
66
"surix",

packages/client/src/project/entities.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ApiEntity, expandEntity, wrapEntity, wrapEntityArray, WrappedEntity} from '@surix/data-helpers';
22
import { AxiosInstance } from 'axios';
3-
import { Entity, ProjectEntities, Query } from '../types';
3+
import { DeletedEntities, Entity, ProjectEntities, Query } from '../types';
44

55
export function getProjectEntities (projectId: string, apiClient: AxiosInstance): ProjectEntities {
66
return {
@@ -33,6 +33,20 @@ export function getProjectEntities (projectId: string, apiClient: AxiosInstance)
3333
const returnedEntity = res.data;
3434
return wrapEntity(returnedEntity);
3535
},
36+
async delete(entityId: string): Promise<WrappedEntity> {
37+
const res = await apiClient.delete(`/projects/${projectId}/entities/${entityId}`);
38+
const returnedEntity = res.data;
39+
return wrapEntity(returnedEntity);
40+
},
41+
async deleteMany(entityIds: string[]): Promise<DeletedEntities> {
42+
const res = await apiClient.delete(`/projects/${projectId}/entities`,
43+
{
44+
data: {
45+
entities: entityIds
46+
}
47+
});
48+
return res.data;
49+
},
3650
async create(entity: Entity): Promise<WrappedEntity> {
3751
const expandedEntity = expandEntity(entity);
3852
const res = await apiClient.post<ApiEntity>(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Project Files get should return the wrapped file 1`] = `
4+
Object {
5+
"_id": "f1",
6+
"createdAt": 2017-02-22T09:19:33.885Z,
7+
"createdBy": Object {
8+
"_id": "u1",
9+
"type": "user",
10+
},
11+
"downloadUrl": "http://download/f1",
12+
"filename": "f1.jpeg",
13+
"id": "f1",
14+
"mimeType": "image/jpeg",
15+
"name": "F1",
16+
"public": true,
17+
"rawFile": Object {
18+
"_id": "f1",
19+
"createdAt": "2017-02-22T09:19:33.885Z",
20+
"createdBy": Object {
21+
"_id": "u1",
22+
"type": "user",
23+
},
24+
"downloadUrl": "http://download/f1",
25+
"filename": "f1.jpeg",
26+
"mimeType": "image/jpeg",
27+
"name": "F1",
28+
"public": true,
29+
"size": 200,
30+
"status": "ready",
31+
"tags": Array [
32+
"t1",
33+
],
34+
"updatedAt": "2018-10-22T09:19:33.885Z",
35+
"uploadUrl": "http://upload/f1",
36+
},
37+
"size": 200,
38+
"status": "ready",
39+
"tags": Array [
40+
"t1",
41+
],
42+
"updatedAt": 2018-10-22T09:19:33.885Z,
43+
"uploadUrl": "http://upload/f1",
44+
}
45+
`;
46+
47+
exports[`Project Files list should return the wrapped file array 1`] = `
48+
Array [
49+
Object {
50+
"_id": "f1",
51+
"createdAt": 2017-02-22T09:19:33.885Z,
52+
"createdBy": Object {
53+
"_id": "u1",
54+
"type": "user",
55+
},
56+
"downloadUrl": "http://download/f1",
57+
"filename": "f1.jpeg",
58+
"id": "f1",
59+
"mimeType": "image/jpeg",
60+
"name": "F1",
61+
"public": true,
62+
"rawFile": Object {
63+
"_id": "f1",
64+
"createdAt": "2017-02-22T09:19:33.885Z",
65+
"createdBy": Object {
66+
"_id": "u1",
67+
"type": "user",
68+
},
69+
"downloadUrl": "http://download/f1",
70+
"filename": "f1.jpeg",
71+
"mimeType": "image/jpeg",
72+
"name": "F1",
73+
"public": true,
74+
"size": 200,
75+
"status": "ready",
76+
"tags": Array [
77+
"t1",
78+
],
79+
"updatedAt": "2018-10-22T09:19:33.885Z",
80+
"uploadUrl": "http://upload/f1",
81+
},
82+
"size": 200,
83+
"status": "ready",
84+
"tags": Array [
85+
"t1",
86+
],
87+
"updatedAt": 2018-10-22T09:19:33.885Z,
88+
"uploadUrl": "http://upload/f1",
89+
},
90+
Object {
91+
"_id": "f2",
92+
"createdAt": 2017-02-22T09:19:33.885Z,
93+
"createdBy": Object {
94+
"_id": "u1",
95+
"type": "user",
96+
},
97+
"downloadUrl": "",
98+
"filename": "f2.jpeg",
99+
"id": "f2",
100+
"mimeType": "image/jpeg",
101+
"name": "F2",
102+
"public": true,
103+
"rawFile": Object {
104+
"_id": "f2",
105+
"createdAt": "2017-02-22T09:19:33.885Z",
106+
"createdBy": Object {
107+
"_id": "u1",
108+
"type": "user",
109+
},
110+
"downloadUrl": "",
111+
"filename": "f2.jpeg",
112+
"mimeType": "image/jpeg",
113+
"name": "F2",
114+
"public": true,
115+
"size": 0,
116+
"status": "pending",
117+
"tags": Array [
118+
"t1",
119+
],
120+
"updatedAt": "2018-10-22T09:19:33.885Z",
121+
"uploadUrl": "http://upload/f1",
122+
},
123+
"size": 0,
124+
"status": "pending",
125+
"tags": Array [
126+
"t1",
127+
],
128+
"updatedAt": 2018-10-22T09:19:33.885Z,
129+
"uploadUrl": "http://upload/f1",
130+
},
131+
]
132+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Project tags list should return tags list 1`] = `
4+
Array [
5+
Object {
6+
"name": "posts",
7+
},
8+
Object {
9+
"name": "users",
10+
},
11+
]
12+
`;

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

+36-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { AxiosInstance } from 'axios';
33
import * as api from '../../api';
44
import { getProjectApi } from '../project';
55

6-
import { Query } from '../../types';
6+
import { DeletedEntities, Query } from '../../types';
77

88
describe('Project Entities', () => {
99
const apiEntities = [
@@ -165,6 +165,40 @@ describe('Project Entities', () => {
165165
expect(apiClient.put).toHaveBeenCalledWith(
166166
'/projects/project1/entities/entity1', expectedEntity);
167167
});
168-
});
168+
});
169+
describe('delete', () => {
170+
async function callMockDelete (entityId: string): Promise<dataHelpers.WrappedEntity> {
171+
apiClient = api.getApiClient('http://baseurl', 'somekey');
172+
jest.spyOn(apiClient, 'delete').mockReturnValue(Promise.resolve({ data:
173+
{ ...apiEntities[0] } }));
174+
jest.spyOn(dataHelpers, 'wrapEntity');
175+
const project = getProjectApi('project1', apiClient);
176+
const ent = await project.entities.delete(entityId);
177+
return ent;
178+
}
179+
180+
async function callMockDeleteMany (entityIds: string[]): Promise<DeletedEntities> {
181+
apiClient = api.getApiClient('http://baseurl', 'somekey');
182+
jest.spyOn(apiClient, 'delete').mockReturnValue(Promise.resolve({ data:
183+
{ deleted: entityIds.length } }));
184+
jest.spyOn(dataHelpers, 'wrapEntity');
185+
const project = getProjectApi('project1', apiClient);
186+
const ent = await project.entities.deleteMany(entityIds);
187+
return ent;
188+
}
189+
190+
it('should call DELETE /projects/:pid/entities/:eid', async () => {
191+
const entityId = 'entity1';
192+
await callMockDelete(entityId);
193+
expect(apiClient.delete).toHaveBeenCalledWith(`/projects/project1/entities/${entityId}`);
194+
});
195+
it('should call DELETE /projects/:pid/entities with a list of entity ids', async () => {
196+
const entityIds = ['entity1', 'entity2'];
197+
198+
await callMockDeleteMany(entityIds);
199+
expect(apiClient.delete).toHaveBeenCalledWith(`/projects/project1/entities`,
200+
{ data: { entities: entityIds } });
201+
});
202+
});
169203
});
170204
});

packages/client/src/types.ts

+38
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,51 @@ export interface Project {
77
}
88

99
export interface ProjectEntities {
10+
/**
11+
* Fetches an entity with the given id
12+
* @param id An ID indetifying an entity
13+
* @returns {Promise<WrappedEntity>}
14+
*/
1015
get(id: string): Promise<WrappedEntity>;
16+
/**
17+
* Fetches entities based on the provided query
18+
* @param query A query to be used to fetch entities
19+
* @returns {Promise<WrappedEntity[]>}
20+
*/
1121
query(query?: Query): Promise<WrappedEntity[]>;
22+
/**
23+
* Creates and saves the provided entity
24+
* @param {Entity} entity An entity
25+
* @returns {Promise<WrappedEntity>}
26+
*/
1227
create(entity: Entity): Promise<WrappedEntity>;
28+
/**
29+
* Updates the entity identified by the _id property provided.
30+
* @param {Entity} entity An entity
31+
* @returns {Promise<WrappedEntity>}
32+
*/
1333
update(entity: Entity): Promise<WrappedEntity>;
34+
/**
35+
* Replaces the entity identified by the _id property provided.
36+
* @param {Entity} entity An entity
37+
* @returns {Promise<WrappedEntity>}
38+
*/
1439
put(entity: Entity): Promise<WrappedEntity>;
40+
/**
41+
* Completely removes an etity identified by the id provided.
42+
* @param {string} entityId An ID
43+
*/
44+
delete(entityId: string): Promise<WrappedEntity>;
45+
/**
46+
* Deletes a bunch of entities that have the provided IDs
47+
* @param {EntityIds} entityIds A list of IDs
48+
*/
49+
deleteMany(entityIds: string[]): Promise<DeletedEntities>;
1550
}
1651

52+
export interface DeletedEntities {
53+
deleted: number;
54+
}
1755
export interface ProjectFiles {
1856
get(id: string): Promise<WrappedFile>;
1957
list(): Promise<WrappedFile[]>;

0 commit comments

Comments
 (0)