@creator.co/wapi 1.2.2 → 1.2.3
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.
- package/.eslintrc.cjs +29 -22
- package/.github/workflows/npmpublish.yml +1 -1
- package/.github/workflows/prs.yml +1 -1
- package/index.ts +12 -10
- package/jest.config.ts +19 -19
- package/package.json +1 -1
- package/src/API/Request.ts +17 -35
- package/src/API/Response.ts +24 -42
- package/src/API/Utils.ts +5 -7
- package/src/BaseEvent/EventProcessor.ts +16 -24
- package/src/BaseEvent/Process.ts +7 -12
- package/src/BaseEvent/Transaction.ts +25 -43
- package/src/Config/Configuration.ts +8 -14
- package/src/Config/EnvironmentVar.ts +10 -20
- package/src/Crypto/Crypto.ts +10 -10
- package/src/Crypto/JWT.ts +4 -10
- package/src/Globals.ts +19 -25
- package/src/Logger/Logger.ts +36 -51
- package/src/Mailer/Mailer.ts +19 -31
- package/src/Publisher/Publisher.ts +7 -12
- package/src/Server/RouteResolver.ts +5 -6
- package/src/Server/Router.ts +7 -12
- package/src/Server/lib/ContainerServer.ts +5 -5
- package/src/Server/lib/Server.ts +26 -38
- package/src/Server/lib/container/GenericHandler.ts +8 -13
- package/src/Server/lib/container/GenericHandlerEvent.ts +21 -35
- package/src/Server/lib/container/HealthHandler.ts +2 -2
- package/src/Server/lib/container/Proxy.ts +26 -38
- package/src/Server/lib/container/Utils.ts +2 -2
- package/src/Validation/Validator.ts +6 -6
- package/tests/API/Request.test.ts +107 -111
- package/tests/API/Response.test.ts +86 -91
- package/tests/API/Utils.test.ts +64 -64
- package/tests/BaseEvent/EventProcessor.test.ts +68 -84
- package/tests/BaseEvent/Process.test.ts +11 -11
- package/tests/BaseEvent/Transaction.test.ts +44 -53
- package/tests/Config/Config.test.ts +50 -50
- package/tests/Config/EnvironmentVar.test.ts +50 -59
- package/tests/Crypto/Crypto.test.ts +20 -22
- package/tests/Crypto/JWT.test.ts +40 -40
- package/tests/Logger/Logger.test.ts +24 -36
- package/tests/Mailer/Mailer.test.ts +21 -29
- package/tests/Publisher/Publisher.test.ts +18 -18
- package/tests/Server/RouteResolver.test.ts +56 -59
- package/tests/Server/Router.test.ts +16 -16
- package/tests/Server/lib/ContainerServer.test.ts +83 -85
- package/tests/Server/lib/Server.test.ts +4 -4
- package/tests/Server/lib/container/GenericHandler.test.ts +31 -41
- package/tests/Server/lib/container/GenericHandlerEvent.test.ts +35 -36
- package/tests/Server/lib/container/HealthHandler.test.ts +7 -7
- package/tests/Server/lib/container/Proxy.test.ts +66 -79
- package/tests/Server/lib/container/Utils.test.ts +16 -17
- package/tests/Test.utils.ts +9 -9
- package/tests/Validation/Validator.test.ts +28 -40
- package/tests/main.test.ts +2 -2
|
@@ -1,184 +1,180 @@
|
|
|
1
|
-
import { APIGatewayEvent, Context } from
|
|
2
|
-
import { expect } from
|
|
1
|
+
import { APIGatewayEvent, Context } from 'aws-lambda'
|
|
2
|
+
import { expect } from 'chai'
|
|
3
3
|
|
|
4
|
-
import Request from
|
|
5
|
-
import Logger from
|
|
4
|
+
import Request from '../../src/API/Request'
|
|
5
|
+
import Logger from '../../src/Logger/Logger'
|
|
6
6
|
|
|
7
7
|
function newReq(event: any, context?: any) {
|
|
8
8
|
const logger = new Logger(
|
|
9
9
|
{
|
|
10
|
-
logLevel:
|
|
10
|
+
logLevel: 'DEBUG',
|
|
11
11
|
},
|
|
12
|
-
|
|
13
|
-
)
|
|
14
|
-
return new Request(
|
|
15
|
-
<APIGatewayEvent>(<unknown>event),
|
|
16
|
-
<Context>context ? context : {},
|
|
17
|
-
logger,
|
|
12
|
+
'123-456'
|
|
18
13
|
)
|
|
14
|
+
return new Request(<APIGatewayEvent>(<unknown>event), <Context>context ? context : {}, logger)
|
|
19
15
|
}
|
|
20
16
|
|
|
21
|
-
describe(
|
|
22
|
-
test(
|
|
17
|
+
describe('Request querystring', () => {
|
|
18
|
+
test('Null query string', () => {
|
|
23
19
|
const r = newReq({
|
|
24
20
|
queryStringParameters: null,
|
|
25
21
|
})
|
|
26
|
-
expect(r.containsQueryParam(
|
|
27
|
-
expect(r.getQueryParam(
|
|
22
|
+
expect(r.containsQueryParam('123')).to.be.false
|
|
23
|
+
expect(r.getQueryParam('123')).to.be.null
|
|
28
24
|
})
|
|
29
25
|
|
|
30
|
-
test(
|
|
26
|
+
test('Empty query string', () => {
|
|
31
27
|
const r = newReq({
|
|
32
28
|
queryStringParameters: {},
|
|
33
29
|
})
|
|
34
|
-
expect(r.containsQueryParam(
|
|
35
|
-
expect(r.getQueryParam(
|
|
30
|
+
expect(r.containsQueryParam('123')).to.be.false
|
|
31
|
+
expect(r.getQueryParam('123')).to.be.null
|
|
36
32
|
})
|
|
37
33
|
|
|
38
|
-
test(
|
|
34
|
+
test('Valid query string', () => {
|
|
39
35
|
const r = newReq({
|
|
40
|
-
queryStringParameters: {
|
|
36
|
+
queryStringParameters: { '123': 'abc' },
|
|
41
37
|
})
|
|
42
|
-
expect(r.containsQueryParam(
|
|
43
|
-
expect(r.getQueryParam(
|
|
38
|
+
expect(r.containsQueryParam('123')).to.be.true
|
|
39
|
+
expect(r.getQueryParam('123')).to.be.equals('abc')
|
|
44
40
|
})
|
|
45
41
|
|
|
46
|
-
test(
|
|
42
|
+
test('Valid query string number', () => {
|
|
47
43
|
const r = newReq({
|
|
48
|
-
queryStringParameters: {
|
|
44
|
+
queryStringParameters: { '123': 456 },
|
|
49
45
|
})
|
|
50
|
-
expect(r.containsQueryParam(
|
|
51
|
-
expect(r.getQueryParam(
|
|
46
|
+
expect(r.containsQueryParam('123')).to.be.true
|
|
47
|
+
expect(r.getQueryParam('123')).to.be.equals(456)
|
|
52
48
|
})
|
|
53
49
|
})
|
|
54
50
|
|
|
55
|
-
describe(
|
|
56
|
-
test(
|
|
51
|
+
describe('Request headers', () => {
|
|
52
|
+
test('Null headers', () => {
|
|
57
53
|
const r = newReq({ headers: null })
|
|
58
|
-
expect(r.getHeader(
|
|
54
|
+
expect(r.getHeader('123')).to.be.null
|
|
59
55
|
})
|
|
60
56
|
|
|
61
|
-
test(
|
|
57
|
+
test('Empty headers', () => {
|
|
62
58
|
const r = newReq({ headers: {} })
|
|
63
|
-
expect(r.getHeader(
|
|
59
|
+
expect(r.getHeader('123')).to.be.null
|
|
64
60
|
})
|
|
65
61
|
|
|
66
|
-
test(
|
|
67
|
-
const r = newReq({ headers: {
|
|
68
|
-
expect(r.getHeader(
|
|
62
|
+
test('Valid headers', () => {
|
|
63
|
+
const r = newReq({ headers: { '123': 'abc' } })
|
|
64
|
+
expect(r.getHeader('123')).to.be.equals('abc')
|
|
69
65
|
})
|
|
70
66
|
|
|
71
|
-
test(
|
|
72
|
-
const r = newReq({ headers: { Authorization:
|
|
73
|
-
expect(r.getAuthorizationHeader()).to.be.equals(
|
|
67
|
+
test('Auth headers', () => {
|
|
68
|
+
const r = newReq({ headers: { Authorization: 'abc' } })
|
|
69
|
+
expect(r.getAuthorizationHeader()).to.be.equals('abc')
|
|
74
70
|
})
|
|
75
71
|
})
|
|
76
72
|
|
|
77
|
-
describe(
|
|
78
|
-
test(
|
|
73
|
+
describe('Request context', () => {
|
|
74
|
+
test('Null context', () => {
|
|
79
75
|
const r = newReq({ requestContext: null })
|
|
80
|
-
expect(r.getContextParam(
|
|
76
|
+
expect(r.getContextParam('123')).to.be.null
|
|
81
77
|
})
|
|
82
78
|
|
|
83
|
-
test(
|
|
79
|
+
test('Empty context', () => {
|
|
84
80
|
const r = newReq({ requestContext: {} })
|
|
85
|
-
expect(r.getContextParam(
|
|
81
|
+
expect(r.getContextParam('123')).to.be.null
|
|
86
82
|
})
|
|
87
83
|
|
|
88
|
-
test(
|
|
84
|
+
test('Valid context', () => {
|
|
89
85
|
const r = newReq({
|
|
90
|
-
requestContext: {
|
|
86
|
+
requestContext: { '123': 'abc' },
|
|
91
87
|
})
|
|
92
|
-
expect(r.getContextParam(
|
|
88
|
+
expect(r.getContextParam('123')).to.be.equals('abc')
|
|
93
89
|
})
|
|
94
90
|
})
|
|
95
91
|
|
|
96
|
-
describe(
|
|
97
|
-
test(
|
|
92
|
+
describe('Request path params', () => {
|
|
93
|
+
test('Null path params', () => {
|
|
98
94
|
const r = newReq({ pathParameters: null })
|
|
99
|
-
expect(r.containsPathParam(
|
|
100
|
-
expect(r.getPathParam(
|
|
95
|
+
expect(r.containsPathParam('123')).to.be.false
|
|
96
|
+
expect(r.getPathParam('123')).to.be.null
|
|
101
97
|
expect(r.getPathParams()).to.be.null
|
|
102
98
|
})
|
|
103
99
|
|
|
104
|
-
test(
|
|
100
|
+
test('Empty path params', () => {
|
|
105
101
|
const r = newReq({ pathParameters: {} })
|
|
106
|
-
expect(r.containsPathParam(
|
|
107
|
-
expect(r.getPathParam(
|
|
102
|
+
expect(r.containsPathParam('123')).to.be.false
|
|
103
|
+
expect(r.getPathParam('123')).to.be.null
|
|
108
104
|
expect(r.getPathParams()).to.be.deep.equal({})
|
|
109
105
|
})
|
|
110
106
|
|
|
111
|
-
test(
|
|
112
|
-
const v = {
|
|
107
|
+
test('Valid path params', () => {
|
|
108
|
+
const v = { '123': 'abc' }
|
|
113
109
|
const r = newReq({
|
|
114
110
|
pathParameters: v,
|
|
115
111
|
})
|
|
116
|
-
expect(r.containsPathParam(
|
|
117
|
-
expect(r.getPathParam(
|
|
112
|
+
expect(r.containsPathParam('123')).to.be.true
|
|
113
|
+
expect(r.getPathParam('123')).to.be.equals('abc')
|
|
118
114
|
expect(r.getPathParams()).to.be.deep.equal(v)
|
|
119
115
|
})
|
|
120
116
|
|
|
121
|
-
test(
|
|
122
|
-
const v = {
|
|
117
|
+
test('Valid path param number', () => {
|
|
118
|
+
const v = { '123': 456 }
|
|
123
119
|
const r = newReq({
|
|
124
120
|
pathParameters: v,
|
|
125
121
|
})
|
|
126
|
-
expect(r.containsPathParam(
|
|
127
|
-
expect(r.getPathParam(
|
|
122
|
+
expect(r.containsPathParam('123')).to.be.true
|
|
123
|
+
expect(r.getPathParam('123')).to.be.equals(456)
|
|
128
124
|
expect(r.getPathParams()).to.be.deep.equal(v)
|
|
129
125
|
})
|
|
130
126
|
|
|
131
|
-
test(
|
|
132
|
-
const v = {
|
|
127
|
+
test('Fix path params', () => {
|
|
128
|
+
const v = { '123': 'abc' }
|
|
133
129
|
const r = newReq({ pathParameters: null })
|
|
134
130
|
r.setFixedPathParams(
|
|
135
|
-
Object.keys(v).map(
|
|
136
|
-
[
|
|
131
|
+
Object.keys(v).map(k => ({ name: k })),
|
|
132
|
+
['/'].concat(Object.values(v))
|
|
137
133
|
)
|
|
138
|
-
expect(r.containsPathParam(
|
|
139
|
-
expect(r.getPathParam(
|
|
134
|
+
expect(r.containsPathParam('123')).to.be.true
|
|
135
|
+
expect(r.getPathParam('123')).to.be.equals('abc')
|
|
140
136
|
expect(r.getPathParams()).to.be.deep.equal(v)
|
|
141
137
|
})
|
|
142
138
|
})
|
|
143
139
|
|
|
144
|
-
describe(
|
|
145
|
-
test(
|
|
140
|
+
describe('Request body', () => {
|
|
141
|
+
test('Null body', () => {
|
|
146
142
|
const r = newReq({ body: null })
|
|
147
143
|
expect(r.getBody()).to.be.null
|
|
148
144
|
})
|
|
149
145
|
|
|
150
|
-
test(
|
|
146
|
+
test('Empty body', () => {
|
|
151
147
|
const r = newReq({ body: {} })
|
|
152
148
|
expect(r.getBody()).to.be.deep.equals({})
|
|
153
149
|
})
|
|
154
150
|
|
|
155
|
-
test(
|
|
156
|
-
const r = newReq({ body:
|
|
151
|
+
test('Empty string body', () => {
|
|
152
|
+
const r = newReq({ body: '{}' })
|
|
157
153
|
expect(r.getBody()).to.be.deep.equals({})
|
|
158
154
|
})
|
|
159
155
|
|
|
160
|
-
test(
|
|
156
|
+
test('Broken json string body', () => {
|
|
161
157
|
const r = newReq({ body: '{name": "Joe"}' })
|
|
162
158
|
expect(r.getBody()).to.be.equals('{name": "Joe"}')
|
|
163
159
|
})
|
|
164
160
|
|
|
165
|
-
test(
|
|
161
|
+
test('Valid string body', () => {
|
|
166
162
|
const r = newReq({ body: '{"name": "Joe"}' })
|
|
167
163
|
expect(r.getBody()).to.be.deep.equals({
|
|
168
|
-
name:
|
|
164
|
+
name: 'Joe',
|
|
169
165
|
})
|
|
170
166
|
})
|
|
171
167
|
|
|
172
|
-
test(
|
|
173
|
-
const r = newReq({ body: { name:
|
|
168
|
+
test('Valid json body', () => {
|
|
169
|
+
const r = newReq({ body: { name: 'Joe' } })
|
|
174
170
|
expect(r.getBody()).to.be.deep.equals({
|
|
175
|
-
name:
|
|
171
|
+
name: 'Joe',
|
|
176
172
|
})
|
|
177
173
|
})
|
|
178
174
|
})
|
|
179
175
|
|
|
180
|
-
describe(
|
|
181
|
-
test(
|
|
176
|
+
describe('Request path/method', () => {
|
|
177
|
+
test('Null path/method', () => {
|
|
182
178
|
const r = newReq({
|
|
183
179
|
path: null,
|
|
184
180
|
httpMethod: null,
|
|
@@ -187,76 +183,76 @@ describe("Request path/method", () => {
|
|
|
187
183
|
expect(r.getMethod()).to.be.null
|
|
188
184
|
})
|
|
189
185
|
|
|
190
|
-
test(
|
|
186
|
+
test('Empty path/method', () => {
|
|
191
187
|
const r = newReq({
|
|
192
|
-
path:
|
|
193
|
-
httpMethod:
|
|
188
|
+
path: '',
|
|
189
|
+
httpMethod: '',
|
|
194
190
|
})
|
|
195
|
-
expect(r.getPath()).to.be.a(
|
|
196
|
-
expect(r.getPath()).to.be.equals(
|
|
197
|
-
expect(r.getMethod()).to.be.a(
|
|
198
|
-
expect(r.getMethod()).to.be.equals(
|
|
191
|
+
expect(r.getPath()).to.be.a('string')
|
|
192
|
+
expect(r.getPath()).to.be.equals('')
|
|
193
|
+
expect(r.getMethod()).to.be.a('string')
|
|
194
|
+
expect(r.getMethod()).to.be.equals('')
|
|
199
195
|
})
|
|
200
196
|
|
|
201
|
-
test(
|
|
197
|
+
test('Valid method/path', () => {
|
|
202
198
|
const r = newReq({
|
|
203
|
-
path:
|
|
204
|
-
httpMethod:
|
|
199
|
+
path: '/root',
|
|
200
|
+
httpMethod: 'GET',
|
|
205
201
|
})
|
|
206
|
-
expect(r.getPath()).to.be.a(
|
|
207
|
-
expect(r.getPath()).to.be.equals(
|
|
208
|
-
expect(r.getMethod()).to.be.a(
|
|
209
|
-
expect(r.getMethod()).to.be.equals(
|
|
202
|
+
expect(r.getPath()).to.be.a('string')
|
|
203
|
+
expect(r.getPath()).to.be.equals('/root')
|
|
204
|
+
expect(r.getMethod()).to.be.a('string')
|
|
205
|
+
expect(r.getMethod()).to.be.equals('GET')
|
|
210
206
|
})
|
|
211
207
|
})
|
|
212
208
|
|
|
213
|
-
describe(
|
|
214
|
-
test(
|
|
215
|
-
const r = newReq({}, { awsRequestId:
|
|
209
|
+
describe('Request ID', () => {
|
|
210
|
+
test('From context', () => {
|
|
211
|
+
const r = newReq({}, { awsRequestId: '123' })
|
|
216
212
|
expect(r.getRequestID()).to.not.be.null
|
|
217
|
-
expect(r.getRequestID()).to.be.equals(
|
|
213
|
+
expect(r.getRequestID()).to.be.equals('123')
|
|
218
214
|
})
|
|
219
215
|
|
|
220
|
-
test(
|
|
216
|
+
test('From event', () => {
|
|
221
217
|
const r = newReq({
|
|
222
|
-
requestContext: { requestId:
|
|
218
|
+
requestContext: { requestId: '123' },
|
|
223
219
|
})
|
|
224
220
|
expect(r.getRequestID()).to.not.be.null
|
|
225
|
-
expect(r.getRequestID()).to.be.equals(
|
|
221
|
+
expect(r.getRequestID()).to.be.equals('123')
|
|
226
222
|
})
|
|
227
223
|
|
|
228
|
-
test(
|
|
224
|
+
test('unknown', () => {
|
|
229
225
|
const r = newReq({})
|
|
230
226
|
expect(r.getRequestID()).to.not.be.null
|
|
231
|
-
expect(r.getRequestID()).to.be.equals(
|
|
227
|
+
expect(r.getRequestID()).to.be.equals('unknown')
|
|
232
228
|
})
|
|
233
229
|
})
|
|
234
230
|
|
|
235
|
-
describe(
|
|
236
|
-
test(
|
|
231
|
+
describe('Request origin IP', () => {
|
|
232
|
+
test('From context', () => {
|
|
237
233
|
const r = newReq({
|
|
238
234
|
requestContext: {
|
|
239
|
-
identity: { sourceIp:
|
|
235
|
+
identity: { sourceIp: '127.0.0.1' },
|
|
240
236
|
},
|
|
241
237
|
})
|
|
242
238
|
expect(r.getOriginIP()).to.not.be.null
|
|
243
|
-
expect(r.getOriginIP()).to.be.equals(
|
|
239
|
+
expect(r.getOriginIP()).to.be.equals('127.0.0.1')
|
|
244
240
|
})
|
|
245
241
|
|
|
246
|
-
test(
|
|
242
|
+
test('From header', () => {
|
|
247
243
|
const r = newReq({
|
|
248
244
|
headers: {
|
|
249
|
-
|
|
245
|
+
'X-Forwarded-For': '127.0.0.1',
|
|
250
246
|
},
|
|
251
247
|
})
|
|
252
248
|
expect(r.getOriginIP()).to.not.be.null
|
|
253
|
-
expect(r.getOriginIP()).to.be.equals(
|
|
249
|
+
expect(r.getOriginIP()).to.be.equals('127.0.0.1')
|
|
254
250
|
})
|
|
255
251
|
|
|
256
|
-
test(
|
|
252
|
+
test('unknown', () => {
|
|
257
253
|
const r = newReq({})
|
|
258
254
|
expect(r.getOriginIP()).to.not.be.null
|
|
259
|
-
expect(r.getOriginIP()).to.be.equals(
|
|
255
|
+
expect(r.getOriginIP()).to.be.equals('unknown')
|
|
260
256
|
})
|
|
261
257
|
})
|
|
262
258
|
|