@furystack/rest-service 6.2.0 → 6.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/actions/error-action.js +5 -5
  2. package/dist/actions/error-action.js.map +1 -1
  3. package/dist/actions/error-action.spec.js +0 -3
  4. package/dist/actions/error-action.spec.js.map +1 -1
  5. package/dist/endpoint-generators/create-delete-endpoint.spec.js +4 -7
  6. package/dist/endpoint-generators/create-delete-endpoint.spec.js.map +1 -1
  7. package/dist/endpoint-generators/create-get-collection-endpoint.spec.js +15 -14
  8. package/dist/endpoint-generators/create-get-collection-endpoint.spec.js.map +1 -1
  9. package/dist/endpoint-generators/create-get-entity-endpoint.spec.js +12 -18
  10. package/dist/endpoint-generators/create-get-entity-endpoint.spec.js.map +1 -1
  11. package/dist/endpoint-generators/create-patch-endpoint.spec.js +4 -7
  12. package/dist/endpoint-generators/create-patch-endpoint.spec.js.map +1 -1
  13. package/dist/endpoint-generators/create-post-endpoint.spec.js +4 -7
  14. package/dist/endpoint-generators/create-post-endpoint.spec.js.map +1 -1
  15. package/dist/rest-service.integration.spec.js +34 -24
  16. package/dist/rest-service.integration.spec.js.map +1 -1
  17. package/dist/rest.integration.test.js +11 -11
  18. package/dist/rest.integration.test.js.map +1 -1
  19. package/dist/static-server-manager.js +1 -1
  20. package/dist/static-server-manager.js.map +1 -1
  21. package/dist/static-server-manager.spec.js +80 -68
  22. package/dist/static-server-manager.spec.js.map +1 -1
  23. package/dist/validate.integration.spec.js +32 -37
  24. package/dist/validate.integration.spec.js.map +1 -1
  25. package/package.json +11 -12
  26. package/src/actions/error-action.spec.ts +0 -3
  27. package/src/actions/error-action.ts +6 -6
  28. package/src/endpoint-generators/create-delete-endpoint.spec.ts +4 -4
  29. package/src/endpoint-generators/create-get-collection-endpoint.spec.ts +17 -11
  30. package/src/endpoint-generators/create-get-entity-endpoint.spec.ts +12 -16
  31. package/src/endpoint-generators/create-patch-endpoint.spec.ts +4 -4
  32. package/src/endpoint-generators/create-post-endpoint.spec.ts +4 -4
  33. package/src/rest-service.integration.spec.ts +36 -29
  34. package/src/rest.integration.test.ts +10 -10
  35. package/src/static-server-manager.spec.ts +86 -45
  36. package/src/static-server-manager.ts +3 -1
  37. package/src/validate.integration.spec.ts +31 -36
  38. package/types/actions/error-action.d.ts +0 -2
  39. package/types/actions/error-action.d.ts.map +1 -1
  40. package/types/static-server-manager.d.ts.map +1 -1
  41. package/types/validate.integration.spec.d.ts.map +1 -1
@@ -2,7 +2,6 @@ import { usingAsync } from '@furystack/utils'
2
2
  import { Injector } from '@furystack/inject'
3
3
  import type { DeleteEndpoint } from '@furystack/rest'
4
4
  import { createDeleteEndpoint } from './create-delete-endpoint'
5
- import got from 'got'
6
5
  import { MockClass, setupContext } from './utils'
7
6
  import { useRestService } from '../helpers'
8
7
  import { getDataSetFor } from '@furystack/repository'
@@ -26,9 +25,10 @@ describe('createDeleteEndpoint', () => {
26
25
  const countBeforeDelete = await getDataSetFor(i, MockClass, 'id').count(i)
27
26
  expect(countBeforeDelete).toBe(1)
28
27
 
29
- const response = await got('http://127.0.0.1:1111/api/mock', { method: 'DELETE' })
30
- expect(response.statusCode).toBe(204)
31
- expect(response.body).toBe('')
28
+ const response = await fetch('http://127.0.0.1:1111/api/mock', { method: 'DELETE' })
29
+ expect(response.status).toBe(204)
30
+ const txt = await response.text()
31
+ expect(txt).toBe('')
32
32
 
33
33
  const countAfterDelete = await getDataSetFor(i, MockClass, 'id').count(i)
34
34
  expect(countAfterDelete).toBe(0)
@@ -1,6 +1,5 @@
1
1
  import { usingAsync } from '@furystack/utils'
2
2
  import { Injector } from '@furystack/inject'
3
- import got from 'got'
4
3
  import { MockClass, setupContext } from './utils'
5
4
  import { createGetCollectionEndpoint } from './create-get-collection-endpoint'
6
5
  import type { GetCollectionEndpoint, GetCollectionResult } from '@furystack/rest'
@@ -39,8 +38,9 @@ describe('createGetCollectionEndpoint', () => {
39
38
  const count = await getDataSetFor(i, MockClass, 'id').count(i)
40
39
  const allEntities = await getDataSetFor(i, MockClass, 'id').find(i, {})
41
40
 
42
- const response = await got('http://127.0.0.1:1112/api/entities', { method: 'GET' })
43
- const json: GetCollectionResult<MockClass> = JSON.parse(response.body)
41
+ const response = await fetch('http://127.0.0.1:1112/api/entities', { method: 'GET' })
42
+ expect(response.status).toBe(200)
43
+ const json: GetCollectionResult<MockClass> = await response.json()
44
44
  expect(json.count).toBe(count)
45
45
  expect(json.entries).toEqual(allEntities)
46
46
  })
@@ -63,10 +63,11 @@ describe('createGetCollectionEndpoint', () => {
63
63
  const findOptions: FindOptions<MockClass, Array<keyof MockClass>> = { order: { value: 'ASC' } }
64
64
  const count = await getDataSetFor(i, MockClass, 'id').count(i, findOptions.filter)
65
65
  const orderedEntities = await getDataSetFor(i, MockClass, 'id').find(i, findOptions)
66
- const response = await got(`http://127.0.0.1:1113/api/entities?${serializeToQueryString({ findOptions })}`, {
66
+ const response = await fetch(`http://127.0.0.1:1113/api/entities?${serializeToQueryString({ findOptions })}`, {
67
67
  method: 'GET',
68
68
  })
69
- const json: GetCollectionResult<MockClass> = JSON.parse(response.body)
69
+ expect(response.status).toBe(200)
70
+ const json: GetCollectionResult<MockClass> = await response.json()
70
71
  expect(json.count).toBe(count)
71
72
  expect(json.entries).toEqual(orderedEntities)
72
73
  })
@@ -95,10 +96,11 @@ describe('createGetCollectionEndpoint', () => {
95
96
 
96
97
  expect(filteredEntities).not.toContainEqual({ id: 'mock2', value: '3' })
97
98
 
98
- const response = await got(`http://127.0.0.1:1113/api/entities?${serializeToQueryString({ findOptions })}`, {
99
+ const response = await fetch(`http://127.0.0.1:1113/api/entities?${serializeToQueryString({ findOptions })}`, {
99
100
  method: 'GET',
100
101
  })
101
- const json: GetCollectionResult<MockClass> = JSON.parse(response.body)
102
+ expect(response.status).toBe(200)
103
+ const json: GetCollectionResult<MockClass> = await response.json()
102
104
  expect(json.count).toBe(count)
103
105
  expect(json.entries).toEqual(filteredEntities)
104
106
  })
@@ -127,10 +129,13 @@ describe('createGetCollectionEndpoint', () => {
127
129
 
128
130
  selectedEntities.forEach((e) => expect(e.value).toBeUndefined())
129
131
 
130
- const response = await got(`http://127.0.0.1:1113/api/entities?${serializeToQueryString({ findOptions })}`, {
132
+ const response = await fetch(`http://127.0.0.1:1113/api/entities?${serializeToQueryString({ findOptions })}`, {
131
133
  method: 'GET',
132
134
  })
133
- const json: GetCollectionResult<MockClass> = JSON.parse(response.body)
135
+
136
+ expect(response.status).toBe(200)
137
+
138
+ const json: GetCollectionResult<MockClass> = await response.json()
134
139
  expect(json.count).toBe(count)
135
140
  expect(json.entries).toEqual(selectedEntities)
136
141
  })
@@ -161,10 +166,11 @@ describe('createGetCollectionEndpoint', () => {
161
166
  expect(topSkipEntities).not.toContainEqual({ id: 'mock1', value: '4' })
162
167
  expect(topSkipEntities).not.toContainEqual({ id: 'mock4', value: '1' })
163
168
 
164
- const response = await got(`http://127.0.0.1:1113/api/entities?${serializeToQueryString({ findOptions })}`, {
169
+ const response = await fetch(`http://127.0.0.1:1113/api/entities?${serializeToQueryString({ findOptions })}`, {
165
170
  method: 'GET',
166
171
  })
167
- const json: GetCollectionResult<MockClass> = JSON.parse(response.body)
172
+ expect(response.status).toBe(200)
173
+ const json: GetCollectionResult<MockClass> = await response.json()
168
174
  expect(json.count).toBe(count)
169
175
  expect(json.entries).toEqual(topSkipEntities)
170
176
  })
@@ -2,8 +2,6 @@ import { usingAsync } from '@furystack/utils'
2
2
  import { Injector } from '@furystack/inject'
3
3
  import type { GetEntityEndpoint } from '@furystack/rest'
4
4
  import { serializeToQueryString } from '@furystack/rest'
5
- import type { HTTPError } from 'got'
6
- import got from 'got'
7
5
  import { MockClass, setupContext } from './utils'
8
6
  import { createGetEntityEndpoint } from './create-get-entity-endpoint'
9
7
  import { getDataSetFor } from '@furystack/repository'
@@ -26,8 +24,10 @@ describe('createGetEntityEndpoint', () => {
26
24
  const mockEntity: MockClass = { id: 'mock', value: 'mock' }
27
25
  await getDataSetFor(i, MockClass, 'id').add(i, mockEntity)
28
26
 
29
- const response = await got('http://127.0.0.1:1113/api/mock', { method: 'GET' })
30
- expect(JSON.parse(response.body)).toStrictEqual(mockEntity)
27
+ const response = await fetch('http://127.0.0.1:1113/api/mock', { method: 'GET' })
28
+ expect(response.status).toBe(200)
29
+ const body = await response.json()
30
+ expect(body).toEqual(mockEntity)
31
31
  })
32
32
  })
33
33
 
@@ -47,10 +47,12 @@ describe('createGetEntityEndpoint', () => {
47
47
  const mockEntity: MockClass = { id: 'mock', value: 'mock' }
48
48
  await getDataSetFor(i, MockClass, 'id').add(i, mockEntity)
49
49
 
50
- const response = await got(`http://127.0.0.1:1114/api/mock?${serializeToQueryString({ select: ['id'] })}`, {
50
+ const response = await fetch(`http://127.0.0.1:1114/api/mock?${serializeToQueryString({ select: ['id'] })}`, {
51
51
  method: 'GET',
52
52
  })
53
- expect(JSON.parse(response.body)).toStrictEqual({ id: mockEntity.id })
53
+ expect(response.status).toBe(200)
54
+ const body = await response.json()
55
+ expect(body).toEqual({ id: mockEntity.id })
54
56
  })
55
57
  })
56
58
 
@@ -67,16 +69,10 @@ describe('createGetEntityEndpoint', () => {
67
69
  },
68
70
  },
69
71
  })
70
- await new Promise<void>((resolve, reject) => {
71
- got(`http://127.0.0.1:1115/api/mock`, { method: 'GET' })
72
- .then(() => reject('Should throw'))
73
- .catch((err) => {
74
- const e: HTTPError = err
75
- expect(e.response.statusCode).toBe(404)
76
- expect(JSON.parse(e.response.body as string).message).toBe('Entity not found')
77
- resolve()
78
- })
79
- })
72
+ const result = await fetch(`http://127.0.0.1:1115/api/mock`, { method: 'GET' })
73
+ expect(result.status).toBe(404)
74
+ const body = await result.json()
75
+ expect(body).toEqual({ message: 'Entity not found' })
80
76
  })
81
77
  })
82
78
  })
@@ -2,7 +2,6 @@ import { usingAsync } from '@furystack/utils'
2
2
  import { Injector } from '@furystack/inject'
3
3
  import type { PatchEndpoint } from '@furystack/rest'
4
4
  import { createPatchEndpoint } from './create-patch-endpoint'
5
- import got from 'got'
6
5
  import { MockClass, setupContext } from './utils'
7
6
  import { getDataSetFor } from '@furystack/repository'
8
7
  import { useRestService } from '../helpers'
@@ -26,12 +25,13 @@ describe('createPatchEndpoint', () => {
26
25
  const countBeforeDelete = await getDataSetFor(i, MockClass, 'id').count(i)
27
26
  expect(countBeforeDelete).toBe(1)
28
27
 
29
- const response = await got('http://127.0.0.1:1116/api/mock', {
28
+ const response = await fetch('http://127.0.0.1:1116/api/mock', {
30
29
  method: 'PATCH',
31
30
  body: JSON.stringify({ value: 'updated' }),
32
31
  })
33
- expect(response.statusCode).toBe(200)
34
- expect(response.body).toBe('{}')
32
+ expect(response.status).toBe(200)
33
+ const body = await response.json()
34
+ expect(body).toEqual({})
35
35
  const updated = await getDataSetFor(i, MockClass, 'id').get(i, 'mock')
36
36
  expect(updated?.value).toBe('updated')
37
37
  })
@@ -2,7 +2,6 @@ import { usingAsync } from '@furystack/utils'
2
2
  import { Injector } from '@furystack/inject'
3
3
  import type { PostEndpoint } from '@furystack/rest'
4
4
  import { createPostEndpoint } from './create-post-endpoint'
5
- import got from 'got'
6
5
  import { MockClass, setupContext } from './utils'
7
6
  import { useRestService } from '../helpers'
8
7
  import { getDataSetFor } from '@furystack/repository'
@@ -22,12 +21,13 @@ describe('createPostEndpoint', () => {
22
21
  },
23
22
  })
24
23
  const entityToPost = { id: 'mock', value: 'posted' }
25
- const response = await got('http://127.0.0.1:1117/api/mock', {
24
+ const response = await fetch('http://127.0.0.1:1117/api/mock', {
26
25
  method: 'POST',
27
26
  body: JSON.stringify(entityToPost),
28
27
  })
29
- expect(response.statusCode).toBe(201)
30
- expect(JSON.parse(response.body)).toStrictEqual(entityToPost)
28
+ expect(response.status).toBe(201)
29
+ const body = await response.json()
30
+ expect(body).toEqual(entityToPost)
31
31
  const posted = await getDataSetFor(i, MockClass, 'id').get(i, entityToPost.id)
32
32
  expect(posted?.value).toBe('posted')
33
33
  })
@@ -5,7 +5,6 @@ import { GetCurrentUser, IsAuthenticated, LoginAction, LogoutAction } from './ac
5
5
  import type { RestApi } from '@furystack/rest'
6
6
  import { User, InMemoryStore, addStore } from '@furystack/core'
7
7
  import { DefaultSession } from './models/default-session'
8
- import got from 'got'
9
8
  import { JsonResult } from './request-action-implementation'
10
9
  import { useHttpAuthentication, useRestService } from './helpers'
11
10
 
@@ -81,87 +80,95 @@ describe('@furystack/rest-service inregration tests', () => {
81
80
  it('Should respond with 404 when a route is not found', async () => {
82
81
  await usingAsync(new Injector(), async (i) => {
83
82
  await prepareInjector(i)
84
- await expect(got(PathHelper.joinPaths(apiUrl, 'some-route-that-does-not-exists'))).rejects.toThrow(
85
- 'Response code 404 (Not Found)',
86
- )
83
+ const result = await fetch(PathHelper.joinPaths(apiUrl, 'some-route-that-does-not-exists'))
84
+ expect(result.ok).toBe(false)
85
+ expect(result.status).toBe(404)
86
+ const responseText = await result.json()
87
+ expect(responseText).toEqual({ error: 'Content not found' })
87
88
  })
88
89
  })
89
90
 
90
91
  it('Should respond with 401 for unauthorized request errors', async () => {
91
92
  await usingAsync(new Injector(), async (i) => {
92
93
  await prepareInjector(i)
93
- await expect(got(PathHelper.joinPaths(apiUrl, 'currentUser'), { retry: 0 })).rejects.toThrow(
94
- 'Response code 401 (Unauthorized)',
95
- )
94
+ const result = await fetch(PathHelper.joinPaths(apiUrl, 'currentUser'))
95
+ expect(result.ok).toBe(false)
96
+ expect(result.status).toBe(401)
97
+ const responseText = await result.json()
98
+ expect(responseText).toEqual({ error: 'unauthorized' })
96
99
  })
97
100
  })
98
101
 
99
102
  it('Should respond with 401 for unauthorized request errors', async () => {
100
103
  await usingAsync(new Injector(), async (i) => {
101
104
  await prepareInjector(i)
102
- await expect(got(PathHelper.joinPaths(apiUrl, 'currentUser'), { retry: 0 })).rejects.toThrow(
103
- 'Response code 401 (Unauthorized)',
104
- )
105
+ const result = await fetch(PathHelper.joinPaths(apiUrl, 'currentUser'))
106
+ expect(result.ok).toBe(false)
107
+ expect(result.status).toBe(401)
108
+ const responseText = await result.json()
109
+ expect(responseText).toEqual({ error: 'unauthorized' })
105
110
  })
106
111
  })
107
112
 
108
113
  it('Should respond with the correct result body', async () => {
109
114
  await usingAsync(new Injector(), async (i) => {
110
115
  await prepareInjector(i)
111
- const response = await got(PathHelper.joinPaths(apiUrl, 'isAuthenticated'), { retry: 0 })
112
- expect(response.statusCode).toBe(200)
113
- expect(JSON.parse(response.body)).toStrictEqual({ isAuthenticated: false })
116
+ const response = await fetch(PathHelper.joinPaths(apiUrl, 'isAuthenticated'))
117
+ expect(response.status).toBe(200)
118
+ const result = await response.json()
119
+ expect(result).toEqual({ isAuthenticated: false })
114
120
  })
115
121
  })
116
122
 
117
123
  it('Should be able to read query parameters', async () => {
118
124
  await usingAsync(new Injector(), async (i) => {
119
125
  await prepareInjector(i)
120
- const response = await got(PathHelper.joinPaths(apiUrl, 'testQuery?param1=foo'), { retry: 0 })
121
- expect(response.statusCode).toBe(200)
122
- expect(JSON.parse(response.body)).toStrictEqual({ param1Value: 'foo' })
126
+ const response = await fetch(PathHelper.joinPaths(apiUrl, 'testQuery?param1=foo'))
127
+ expect(response.status).toBe(200)
128
+ const result = await response.json()
129
+ expect(result).toEqual({ param1Value: 'foo' })
123
130
  })
124
131
  })
125
132
 
126
133
  it('Should be able to read url parameters', async () => {
127
134
  await usingAsync(new Injector(), async (i) => {
128
135
  await prepareInjector(i)
129
- const response = await got(PathHelper.joinPaths(apiUrl, 'testUrlParams/bar'), { retry: 0 })
130
- expect(response.statusCode).toBe(200)
131
- expect(JSON.parse(response.body)).toStrictEqual({ urlParamValue: 'bar' })
136
+ const response = await fetch(PathHelper.joinPaths(apiUrl, 'testUrlParams/bar'))
137
+ expect(response.status).toBe(200)
138
+ const result = await response.json()
139
+ expect(result).toEqual({ urlParamValue: 'bar' })
132
140
  })
133
141
  })
134
142
 
135
143
  it('Should be able to read post body', async () => {
136
144
  await usingAsync(new Injector(), async (i) => {
137
145
  await prepareInjector(i)
138
- const response = await got(PathHelper.joinPaths(apiUrl, 'testPostBody'), {
146
+ const response = await fetch(PathHelper.joinPaths(apiUrl, 'testPostBody'), {
139
147
  method: 'POST',
140
148
  body: JSON.stringify({ value: 'baz' }),
141
- retry: 0,
142
149
  })
143
- expect(response.statusCode).toBe(200)
144
- expect(JSON.parse(response.body)).toStrictEqual({ bodyValue: 'baz', body2Value: 'baz' })
150
+ expect(response.status).toBe(200)
151
+ const result = await response.json()
152
+ expect(result).toEqual({ bodyValue: 'baz', body2Value: 'baz' })
145
153
  })
146
154
  })
147
155
 
148
156
  it('Should respond with OK to OPTIONS requests', async () => {
149
157
  await usingAsync(new Injector(), async (i) => {
150
158
  await prepareInjector(i)
151
- const response = await got(PathHelper.joinPaths(apiUrl, 'testPostBody'), {
159
+ const response = await fetch(PathHelper.joinPaths(apiUrl, 'testPostBody'), {
152
160
  method: 'OPTIONS',
153
- retry: 0,
154
161
  })
155
- expect(response.statusCode).toBe(200)
162
+ expect(response.status).toBe(200)
156
163
  })
157
164
  })
158
165
 
159
166
  it('Should reject requests outside of the API Root', async () => {
160
167
  await usingAsync(new Injector(), async (i) => {
161
168
  await prepareInjector(i)
162
- await expect(
163
- got(PathHelper.joinPaths(`http://${hostName}:${port}`, 'not-my-api-root'), { retry: 0 }),
164
- ).rejects.toThrowError('socket hang up')
169
+ await expect(fetch(PathHelper.joinPaths(`http://${hostName}:${port}`, 'not-my-api-root'))).rejects.toThrowError(
170
+ 'fetch failed',
171
+ )
165
172
  })
166
173
  })
167
174
  })
@@ -1,6 +1,6 @@
1
1
  import { Injector } from '@furystack/inject'
2
2
  import type { RestApi } from '@furystack/rest'
3
- import { createClient } from '@furystack/rest-client-got'
3
+ import { createClient } from '@furystack/rest-client-fetch'
4
4
  import { usingAsync } from '@furystack/utils'
5
5
  import { JsonResult } from './request-action-implementation'
6
6
  import './helpers'
@@ -52,15 +52,15 @@ const createEchoApiServer = async () => {
52
52
  }
53
53
  }
54
54
 
55
- describe('REST Integration tests with GOT client', () => {
55
+ describe('REST Integration tests with FETCH client', () => {
56
56
  it('Should execute a single parameterless GET query', async () => {
57
57
  await usingAsync(await createEchoApiServer(), async ({ client }) => {
58
58
  const result = await client({
59
59
  method: 'GET',
60
60
  action: '/plain',
61
61
  })
62
- expect(result.response.statusCode).toBe(200)
63
- expect(result.getJson()).toStrictEqual({})
62
+ expect(result.response.status).toBe(200)
63
+ expect(result.result).toEqual({})
64
64
  })
65
65
  })
66
66
 
@@ -74,8 +74,8 @@ describe('REST Integration tests with GOT client', () => {
74
74
  value,
75
75
  },
76
76
  })
77
- expect(result.response.statusCode).toBe(200)
78
- expect(result.getJson().headers.value).toStrictEqual(value)
77
+ expect(result.response.status).toBe(200)
78
+ expect(result.result.headers.value).toEqual(value)
79
79
  })
80
80
  })
81
81
 
@@ -91,8 +91,8 @@ describe('REST Integration tests with GOT client', () => {
91
91
  },
92
92
  },
93
93
  })
94
- expect(result.response.statusCode).toBe(200)
95
- expect(result.getJson().query.someObject.foo).toStrictEqual(value)
94
+ expect(result.response.status).toBe(200)
95
+ expect(result.result.query.someObject.foo).toEqual(value)
96
96
  })
97
97
  })
98
98
 
@@ -106,8 +106,8 @@ describe('REST Integration tests with GOT client', () => {
106
106
  id: value,
107
107
  },
108
108
  })
109
- expect(result.response.statusCode).toBe(200)
110
- expect(result.getJson().url.id).toEqual(value)
109
+ expect(result.response.status).toBe(200)
110
+ expect(result.result.url.id).toEqual(value)
111
111
  })
112
112
  })
113
113
  })