Skip to content

Commit 0e056f9

Browse files
committed
Add post, put, patch and delete method
1 parent 25b3298 commit 0e056f9

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed

src/api/Request.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,34 @@ export default class Request {
3535
return this.request({ method: 'get', url, ...config })
3636
}
3737

38+
/**
39+
* Perform a post request.
40+
*/
41+
post (url: string, data: any = {}, config: Config = {}): Promise<Response> {
42+
return this.request({ method: 'post', url, data, ...config })
43+
}
44+
45+
/**
46+
* Perform a put request.
47+
*/
48+
put (url: string, data: any = {}, config: Config = {}): Promise<Response> {
49+
return this.request({ method: 'put', url, data, ...config })
50+
}
51+
52+
/**
53+
* Perform a patch request.
54+
*/
55+
patch (url: string, data: any = {}, config: Config = {}): Promise<Response> {
56+
return this.request({ method: 'patch', url, data, ...config })
57+
}
58+
59+
/**
60+
* Perform a delete request.
61+
*/
62+
delete (url: string, config: Config = {}): Promise<Response> {
63+
return this.request({ method: 'delete', url, ...config })
64+
}
65+
3866
/**
3967
* Perform an api request.
4068
*/

test/feature/Request_Base.spec.ts

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,172 @@ describe('Feature - Request - Base', () => {
6161

6262
expect(store.state.entities).toEqual(expected)
6363
})
64+
65+
it('`post` can perform a post request', async () => {
66+
mock.onPost('/api/users').reply(200, [
67+
{ id: 1, name: 'John Doe' },
68+
{ id: 2, name: 'Jane Doe' }
69+
])
70+
71+
const store = createStore([User])
72+
73+
await User.api().post('/api/users')
74+
75+
const expected = createState({
76+
users: {
77+
1: { $id: 1, id: 1, name: 'John Doe' },
78+
2: { $id: 2, id: 2, name: 'Jane Doe' }
79+
}
80+
})
81+
82+
expect(store.state.entities).toEqual(expected)
83+
})
84+
85+
it('`post` can perform a post request with additional config', async () => {
86+
mock.onPost('/api/users').reply(200, {
87+
data: [
88+
{ id: 1, name: 'John Doe' },
89+
{ id: 2, name: 'Jane Doe' }
90+
]
91+
})
92+
93+
const store = createStore([User])
94+
95+
await User.api().post('/api/users', {}, { dataKey: 'data' })
96+
97+
const expected = createState({
98+
users: {
99+
1: { $id: 1, id: 1, name: 'John Doe' },
100+
2: { $id: 2, id: 2, name: 'Jane Doe' }
101+
}
102+
})
103+
104+
expect(store.state.entities).toEqual(expected)
105+
})
106+
107+
it('`put` can perform a put request', async () => {
108+
mock.onPut('/api/users').reply(200, [
109+
{ id: 1, name: 'John Doe' },
110+
{ id: 2, name: 'Jane Doe' }
111+
])
112+
113+
const store = createStore([User])
114+
115+
await User.api().put('/api/users')
116+
117+
const expected = createState({
118+
users: {
119+
1: { $id: 1, id: 1, name: 'John Doe' },
120+
2: { $id: 2, id: 2, name: 'Jane Doe' }
121+
}
122+
})
123+
124+
expect(store.state.entities).toEqual(expected)
125+
})
126+
127+
it('`put` can perform a put request with additional config', async () => {
128+
mock.onPut('/api/users').reply(200, {
129+
data: [
130+
{ id: 1, name: 'John Doe' },
131+
{ id: 2, name: 'Jane Doe' }
132+
]
133+
})
134+
135+
const store = createStore([User])
136+
137+
await User.api().put('/api/users', {}, { dataKey: 'data' })
138+
139+
const expected = createState({
140+
users: {
141+
1: { $id: 1, id: 1, name: 'John Doe' },
142+
2: { $id: 2, id: 2, name: 'Jane Doe' }
143+
}
144+
})
145+
146+
expect(store.state.entities).toEqual(expected)
147+
})
148+
149+
it('`patch` can perform a patch request', async () => {
150+
mock.onPatch('/api/users').reply(200, [
151+
{ id: 1, name: 'John Doe' },
152+
{ id: 2, name: 'Jane Doe' }
153+
])
154+
155+
const store = createStore([User])
156+
157+
await User.api().patch('/api/users')
158+
159+
const expected = createState({
160+
users: {
161+
1: { $id: 1, id: 1, name: 'John Doe' },
162+
2: { $id: 2, id: 2, name: 'Jane Doe' }
163+
}
164+
})
165+
166+
expect(store.state.entities).toEqual(expected)
167+
})
168+
169+
it('`patch` can perform a patch request with additional config', async () => {
170+
mock.onPatch('/api/users').reply(200, {
171+
data: [
172+
{ id: 1, name: 'John Doe' },
173+
{ id: 2, name: 'Jane Doe' }
174+
]
175+
})
176+
177+
const store = createStore([User])
178+
179+
await User.api().patch('/api/users', {}, { dataKey: 'data' })
180+
181+
const expected = createState({
182+
users: {
183+
1: { $id: 1, id: 1, name: 'John Doe' },
184+
2: { $id: 2, id: 2, name: 'Jane Doe' }
185+
}
186+
})
187+
188+
expect(store.state.entities).toEqual(expected)
189+
})
190+
191+
it('`delete` can perform a delete request', async () => {
192+
mock.onDelete('/api/users').reply(200, [
193+
{ id: 1, name: 'John Doe' },
194+
{ id: 2, name: 'Jane Doe' }
195+
])
196+
197+
const store = createStore([User])
198+
199+
await User.api().delete('/api/users')
200+
201+
const expected = createState({
202+
users: {
203+
1: { $id: 1, id: 1, name: 'John Doe' },
204+
2: { $id: 2, id: 2, name: 'Jane Doe' }
205+
}
206+
})
207+
208+
expect(store.state.entities).toEqual(expected)
209+
})
210+
211+
it('`delete` can perform a delete request with additional config', async () => {
212+
mock.onDelete('/api/users').reply(200, {
213+
data: [
214+
{ id: 1, name: 'John Doe' },
215+
{ id: 2, name: 'Jane Doe' }
216+
]
217+
})
218+
219+
const store = createStore([User])
220+
221+
await User.api().delete('/api/users', { dataKey: 'data' })
222+
223+
const expected = createState({
224+
users: {
225+
1: { $id: 1, id: 1, name: 'John Doe' },
226+
2: { $id: 2, id: 2, name: 'Jane Doe' }
227+
}
228+
})
229+
230+
expect(store.state.entities).toEqual(expected)
231+
})
64232
})

0 commit comments

Comments
 (0)