@creator.co/wapi 1.7.1-alpha4 → 1.7.2-alpha1

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 (151) hide show
  1. package/.eslintignore +3 -0
  2. package/.eslintrc.cjs +62 -0
  3. package/.github/workflows/npmpublish.yml +11 -0
  4. package/.github/workflows/prs.yml +13 -0
  5. package/dist/index.d.ts +14 -14
  6. package/dist/index.js +14 -14
  7. package/dist/index.js.map +1 -1
  8. package/dist/package-lock.json +2676 -454
  9. package/dist/package.json +2 -1
  10. package/dist/src/API/Request.d.ts +1 -1
  11. package/dist/src/API/Request.js +1 -1
  12. package/dist/src/API/Request.js.map +1 -1
  13. package/dist/src/API/Response.d.ts +1 -1
  14. package/dist/src/API/Response.js +1 -1
  15. package/dist/src/API/Response.js.map +1 -1
  16. package/dist/src/BaseEvent/EventProcessor.d.ts +2 -2
  17. package/dist/src/BaseEvent/EventProcessor.js +3 -3
  18. package/dist/src/BaseEvent/EventProcessor.js.map +1 -1
  19. package/dist/src/BaseEvent/Process.d.ts +3 -3
  20. package/dist/src/BaseEvent/Process.js +2 -2
  21. package/dist/src/BaseEvent/Process.js.map +1 -1
  22. package/dist/src/BaseEvent/Transaction.d.ts +5 -5
  23. package/dist/src/BaseEvent/Transaction.js +6 -6
  24. package/dist/src/BaseEvent/Transaction.js.map +1 -1
  25. package/dist/src/Cache/Redis.d.ts +1 -1
  26. package/dist/src/Cache/Redis.js +1 -1
  27. package/dist/src/Cache/Redis.js.map +1 -1
  28. package/dist/src/Cache/types.d.ts +1 -1
  29. package/dist/src/Config/Configuration.js +1 -1
  30. package/dist/src/Config/Configuration.js.map +1 -1
  31. package/dist/src/Database/Database.d.ts +2 -2
  32. package/dist/src/Database/DatabaseManager.d.ts +4 -4
  33. package/dist/src/Database/DatabaseManager.js +3 -3
  34. package/dist/src/Database/DatabaseManager.js.map +1 -1
  35. package/dist/src/Database/DatabaseTransaction.d.ts +1 -1
  36. package/dist/src/Database/index.d.ts +10 -10
  37. package/dist/src/Database/index.js +6 -6
  38. package/dist/src/Database/index.js.map +1 -1
  39. package/dist/src/Database/integrations/knex/KnexDatabase.d.ts +3 -3
  40. package/dist/src/Database/integrations/knex/KnexDatabase.js +2 -2
  41. package/dist/src/Database/integrations/knex/KnexDatabase.js.map +1 -1
  42. package/dist/src/Database/integrations/knex/KnexTransaction.d.ts +3 -3
  43. package/dist/src/Database/integrations/knex/KnexTransaction.js +1 -1
  44. package/dist/src/Database/integrations/knex/KnexTransaction.js.map +1 -1
  45. package/dist/src/Database/integrations/kysely/KyselyDatabase.d.ts +3 -3
  46. package/dist/src/Database/integrations/kysely/KyselyDatabase.js +2 -2
  47. package/dist/src/Database/integrations/kysely/KyselyDatabase.js.map +1 -1
  48. package/dist/src/Database/integrations/kysely/KyselyTransaction.d.ts +3 -3
  49. package/dist/src/Database/integrations/kysely/KyselyTransaction.js +1 -1
  50. package/dist/src/Database/integrations/kysely/KyselyTransaction.js.map +1 -1
  51. package/dist/src/Database/integrations/pgsql/PostgresDatabase.d.ts +3 -3
  52. package/dist/src/Database/integrations/pgsql/PostgresDatabase.js +2 -2
  53. package/dist/src/Database/integrations/pgsql/PostgresDatabase.js.map +1 -1
  54. package/dist/src/Database/integrations/pgsql/PostgresTransaction.d.ts +3 -3
  55. package/dist/src/Database/integrations/pgsql/PostgresTransaction.js +1 -1
  56. package/dist/src/Database/integrations/pgsql/PostgresTransaction.js.map +1 -1
  57. package/dist/src/Database/types.d.ts +5 -5
  58. package/dist/src/Globals.js +1 -1
  59. package/dist/src/Globals.js.map +1 -1
  60. package/dist/src/Logger/Logger.js +1 -1
  61. package/dist/src/Logger/Logger.js.map +1 -1
  62. package/dist/src/Server/RouteResolver.d.ts +2 -2
  63. package/dist/src/Server/Router.d.ts +3 -3
  64. package/dist/src/Server/Router.js +3 -3
  65. package/dist/src/Server/Router.js.map +1 -1
  66. package/dist/src/Server/lib/ContainerServer.d.ts +3 -3
  67. package/dist/src/Server/lib/ContainerServer.js +2 -2
  68. package/dist/src/Server/lib/ContainerServer.js.map +1 -1
  69. package/dist/src/Server/lib/Server.d.ts +2 -2
  70. package/dist/src/Server/lib/Server.js +4 -4
  71. package/dist/src/Server/lib/Server.js.map +1 -1
  72. package/dist/src/Server/lib/container/GenericHandler.d.ts +1 -1
  73. package/dist/src/Server/lib/container/GenericHandler.js +3 -3
  74. package/dist/src/Server/lib/container/GenericHandler.js.map +1 -1
  75. package/dist/src/Server/lib/container/GenericHandlerEvent.d.ts +1 -1
  76. package/dist/src/Server/lib/container/GenericHandlerEvent.js +2 -2
  77. package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +1 -1
  78. package/dist/src/Server/lib/container/Proxy.d.ts +2 -2
  79. package/dist/src/Server/lib/container/Proxy.js +4 -4
  80. package/dist/src/Server/lib/container/Proxy.js.map +1 -1
  81. package/dist/src/Validation/Validator.d.ts +1 -1
  82. package/dist/src/Validation/Validator.js +2 -2
  83. package/dist/src/Validation/Validator.js.map +1 -1
  84. package/index.ts +14 -14
  85. package/jest.config.ts +37 -0
  86. package/jest.smoke.config.ts +34 -0
  87. package/package.json +2 -1
  88. package/src/API/Request.ts +2 -2
  89. package/src/API/Response.ts +2 -2
  90. package/src/BaseEvent/EventProcessor.ts +3 -3
  91. package/src/BaseEvent/Process.ts +3 -3
  92. package/src/BaseEvent/Transaction.ts +8 -8
  93. package/src/Cache/Redis.ts +2 -2
  94. package/src/Cache/types.ts +1 -1
  95. package/src/Config/Configuration.ts +1 -1
  96. package/src/Database/Database.ts +2 -2
  97. package/src/Database/DatabaseManager.ts +4 -4
  98. package/src/Database/DatabaseTransaction.ts +1 -1
  99. package/src/Database/index.ts +10 -10
  100. package/src/Database/integrations/knex/KnexDatabase.ts +3 -3
  101. package/src/Database/integrations/knex/KnexTransaction.ts +3 -3
  102. package/src/Database/integrations/kysely/KyselyDatabase.ts +3 -3
  103. package/src/Database/integrations/kysely/KyselyTransaction.ts +3 -3
  104. package/src/Database/integrations/pgsql/PostgresDatabase.ts +3 -3
  105. package/src/Database/integrations/pgsql/PostgresTransaction.ts +3 -3
  106. package/src/Database/types.ts +5 -5
  107. package/src/Globals.ts +1 -1
  108. package/src/Logger/Logger.ts +1 -1
  109. package/src/Server/RouteResolver.ts +2 -2
  110. package/src/Server/Router.ts +6 -6
  111. package/src/Server/lib/ContainerServer.ts +3 -3
  112. package/src/Server/lib/Server.ts +6 -6
  113. package/src/Server/lib/container/GenericHandler.ts +4 -4
  114. package/src/Server/lib/container/GenericHandlerEvent.ts +3 -3
  115. package/src/Server/lib/container/Proxy.ts +6 -6
  116. package/src/Validation/Validator.ts +2 -2
  117. package/tests/API/Request.test.ts +273 -0
  118. package/tests/API/Response.test.ts +367 -0
  119. package/tests/API/Utils.test.ts +167 -0
  120. package/tests/BaseEvent/EventProcessor.test.ts +261 -0
  121. package/tests/BaseEvent/Process.test.ts +49 -0
  122. package/tests/BaseEvent/Transaction.test.ts +408 -0
  123. package/tests/Cache/Redis-client.test.ts +90 -0
  124. package/tests/Cache/Redis-cluster.test.ts +100 -0
  125. package/tests/Config/Config.test.ts +205 -0
  126. package/tests/Config/EnvironmentVar.test.ts +250 -0
  127. package/tests/Crypto/Crypto.test.ts +88 -0
  128. package/tests/Crypto/JWT.test.ts +92 -0
  129. package/tests/Database/DatabaseManager.test.ts +71 -0
  130. package/tests/Database/integrations/knex/KnexDatabase.test.ts +76 -0
  131. package/tests/Database/integrations/knex/KnexTransaction.test.ts +149 -0
  132. package/tests/Database/integrations/kysely/KyselyDatabase.test.ts +113 -0
  133. package/tests/Database/integrations/kysely/KyselyTransaction.test.ts +119 -0
  134. package/tests/Database/integrations/pg/PostgresDatabase.test.ts +76 -0
  135. package/tests/Database/integrations/pg/PostgresTransaction.test.ts +118 -0
  136. package/tests/Logger/Logger.test.ts +219 -0
  137. package/tests/Mailer/Mailer.test.ts +59 -0
  138. package/tests/Publisher/Publisher.test.ts +94 -0
  139. package/tests/Server/RouteResolver.test.ts +102 -0
  140. package/tests/Server/Router.test.ts +39 -0
  141. package/tests/Server/lib/ContainerServer.test.ts +531 -0
  142. package/tests/Server/lib/Server.test.ts +12 -0
  143. package/tests/Server/lib/container/GenericHandler.test.ts +131 -0
  144. package/tests/Server/lib/container/GenericHandlerEvent.test.ts +103 -0
  145. package/tests/Server/lib/container/HealthHandler.test.ts +30 -0
  146. package/tests/Server/lib/container/Proxy.test.ts +268 -0
  147. package/tests/Server/lib/container/Utils.test.ts +47 -0
  148. package/tests/Test.utils.ts +74 -0
  149. package/tests/Validation/Validator.test.ts +76 -0
  150. package/tsconfig.json +26 -0
  151. package/tsconfig.smoke.json +26 -0
@@ -1,13 +1,13 @@
1
- import { Database } from './Database'
2
- import { DatabaseManager } from './DatabaseManager'
3
- import { DatabaseTransaction } from './DatabaseTransaction'
4
- import { KnexDatabase } from './integrations/knex/KnexDatabase'
5
- import { KnexTransaction } from './integrations/knex/KnexTransaction'
6
- import { KyselyDatabase } from './integrations/kysely/KyselyDatabase'
7
- import { KyselyTransaction } from './integrations/kysely/KyselyTransaction'
8
- import { PostgresDatabase } from './integrations/pgsql/PostgresDatabase'
9
- import { PostgresTransaction } from './integrations/pgsql/PostgresTransaction'
10
- import { DbConfig } from './types'
1
+ import { Database } from './Database.js'
2
+ import { DatabaseManager } from './DatabaseManager.js'
3
+ import { DatabaseTransaction } from './DatabaseTransaction.js'
4
+ import { KnexDatabase } from './integrations/knex/KnexDatabase.js'
5
+ import { KnexTransaction } from './integrations/knex/KnexTransaction.js'
6
+ import { KyselyDatabase } from './integrations/kysely/KyselyDatabase.js'
7
+ import { KyselyTransaction } from './integrations/kysely/KyselyTransaction.js'
8
+ import { PostgresDatabase } from './integrations/pgsql/PostgresDatabase.js'
9
+ import { PostgresTransaction } from './integrations/pgsql/PostgresTransaction.js'
10
+ import { DbConfig } from './types.js'
11
11
 
12
12
  export {
13
13
  // Abstracts
@@ -1,9 +1,9 @@
1
1
  import knex, { Knex } from 'knex'
2
2
  import knexStringcase from 'knex-stringcase'
3
3
 
4
- import { KnexTransaction, KnexTransactionImpl } from './KnexTransaction'
5
- import { Database } from '../../Database'
6
- import type { DbConfig } from '../../types'
4
+ import { KnexTransaction, KnexTransactionImpl } from './KnexTransaction.js'
5
+ import { Database } from '../../Database.js'
6
+ import type { DbConfig } from '../../types.js'
7
7
 
8
8
  /**
9
9
  * Represents a KnexDatabase class that extends Database and provides methods for interacting with a Knex database.
@@ -1,8 +1,8 @@
1
1
  import { Knex } from 'knex'
2
2
 
3
- import { KnexDatabase } from './KnexDatabase'
4
- import { Database } from '../../Database'
5
- import { DatabaseTransaction } from '../../DatabaseTransaction'
3
+ import { KnexDatabase } from './KnexDatabase.js'
4
+ import { Database } from '../../Database.js'
5
+ import { DatabaseTransaction } from '../../DatabaseTransaction.js'
6
6
 
7
7
  /**
8
8
  * Represents a Knex transaction, combining the functionality of KnexTransactionImpl
@@ -1,9 +1,9 @@
1
1
  import { CamelCasePlugin, Kysely, PostgresDialect } from 'kysely'
2
2
  import * as pg from 'pg'
3
3
 
4
- import { KyselyTransaction, KyselyTransactionImpl } from './KyselyTransaction'
5
- import { Database } from '../../Database'
6
- import type { DbBaseConfig, DbConfig } from '../../types'
4
+ import { KyselyTransaction, KyselyTransactionImpl } from './KyselyTransaction.js'
5
+ import { Database } from '../../Database.js'
6
+ import type { DbBaseConfig, DbConfig } from '../../types.js'
7
7
 
8
8
  /**
9
9
  * Represents a database connection using the Kysely library with support for transactions.
@@ -1,8 +1,8 @@
1
1
  import { Kysely, Transaction } from 'kysely'
2
2
 
3
- import { KyselyDatabase } from './KyselyDatabase'
4
- import { Database } from '../../Database'
5
- import { DatabaseTransaction } from '../../DatabaseTransaction'
3
+ import { KyselyDatabase } from './KyselyDatabase.js'
4
+ import { Database } from '../../Database.js'
5
+ import { DatabaseTransaction } from '../../DatabaseTransaction.js'
6
6
 
7
7
  /**
8
8
  * Represents a transaction for querying a database with a specific schema.
@@ -1,8 +1,8 @@
1
1
  import * as pg from 'pg'
2
2
 
3
- import { PostgresTransaction, PostgresTransactionImpl } from './PostgresTransaction'
4
- import { Database } from '../../Database'
5
- import type { DbBaseConfig, DbConfig } from '../../types'
3
+ import { PostgresTransaction, PostgresTransactionImpl } from './PostgresTransaction.js'
4
+ import { Database } from '../../Database.js'
5
+ import type { DbBaseConfig, DbConfig } from '../../types.js'
6
6
 
7
7
  /**
8
8
  * Represents a Postgres database connection that extends the Database class.
@@ -1,8 +1,8 @@
1
1
  import * as pg from 'pg'
2
2
 
3
- import { PostgresDatabase } from './PostgresDatabase'
4
- import { Database } from '../../Database'
5
- import { DatabaseTransaction } from '../../DatabaseTransaction'
3
+ import { PostgresDatabase } from './PostgresDatabase.js'
4
+ import { Database } from '../../Database.js'
5
+ import { DatabaseTransaction } from '../../DatabaseTransaction.js'
6
6
 
7
7
  /**
8
8
  * A type alias representing a Postgres transaction. It extends the `PostgresTransactionImpl`
@@ -1,8 +1,8 @@
1
- import { Database } from './Database'
2
- import { DATABASES } from './DatabaseManager'
3
- import { KnexDatabase } from './integrations/knex/KnexDatabase'
4
- import { KyselyDatabase } from './integrations/kysely/KyselyDatabase'
5
- import { PostgresDatabase } from './integrations/pgsql/PostgresDatabase'
1
+ import { Database } from './Database.js'
2
+ import { DATABASES } from './DatabaseManager.js'
3
+ import { KnexDatabase } from './integrations/knex/KnexDatabase.js'
4
+ import { KyselyDatabase } from './integrations/kysely/KyselyDatabase.js'
5
+ import { PostgresDatabase } from './integrations/pgsql/PostgresDatabase.js'
6
6
 
7
7
  /**
8
8
  * Represents the possible types of databases available in the system.
package/src/Globals.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import dotenv from 'dotenv'
2
2
 
3
- import Utils from './Util/Utils'
3
+ import Utils from './Util/Utils.js'
4
4
 
5
5
  // important for dev env to load .env file
6
6
  dotenv.config()
@@ -2,7 +2,7 @@ import abind from 'abind'
2
2
  import stringify from 'json-stringify-safe'
3
3
  import stackTrace from 'stack-trace'
4
4
 
5
- import Utils from '../Util/Utils'
5
+ import Utils from '../Util/Utils.js'
6
6
 
7
7
  /**
8
8
  * Enumeration of log levels.
@@ -1,5 +1,5 @@
1
- import { AnyRoute, RouterConfig } from './Router'
2
- import { HttpMethod } from '../API/Request'
1
+ import { AnyRoute, RouterConfig } from './Router.js'
2
+ import { HttpMethod } from '../API/Request.js'
3
3
 
4
4
  /**
5
5
  * Represents a collection of routes that can be added and resolved.
@@ -3,16 +3,16 @@ import { Server as HTTPServer } from 'http'
3
3
  import express from 'express'
4
4
  import { z } from 'zod'
5
5
 
6
- import ContainerServer from './lib/ContainerServer'
7
- import Server from './lib/Server'
8
- import { HttpMethod } from '../API/Request'
9
- import { ResponseErrorType } from '../API/Response'
6
+ import ContainerServer from './lib/ContainerServer.js'
7
+ import Server from './lib/Server.js'
8
+ import { HttpMethod } from '../API/Request.js'
9
+ import { ResponseErrorType } from '../API/Response.js'
10
10
  import Transaction, {
11
11
  TransactionConfig,
12
12
  TransactionExecution,
13
13
  StringMap,
14
- } from '../BaseEvent/Transaction'
15
- import Utils from '../Util/Utils'
14
+ } from '../BaseEvent/Transaction.js'
15
+ import Utils from '../Util/Utils.js'
16
16
 
17
17
  /**
18
18
  * Represents a route in an API.
@@ -1,6 +1,6 @@
1
- import Proxy from './container/Proxy'
2
- import Server from './Server'
3
- import { RouterConfig } from '../Router'
1
+ import Proxy from './container/Proxy.js'
2
+ import Server from './Server.js'
3
+ import { RouterConfig } from '../Router.js'
4
4
 
5
5
  /**
6
6
  * Represents a server container that extends the Server class and handles serverless events.
@@ -1,12 +1,12 @@
1
1
  import { APIGatewayProxyEvent, Context } from 'aws-lambda'
2
2
  import { pathToRegexp } from 'path-to-regexp'
3
3
 
4
- import Request from '../../API/Request'
5
- import Response, { ResponseErrorType } from '../../API/Response'
6
- import Transaction, { StringMap } from '../../BaseEvent/Transaction'
7
- import Validator from '../../Validation/Validator'
8
- import { RouterConfig } from '../Router'
9
- import RouteResolver from '../RouteResolver'
4
+ import Request from '../../API/Request.js'
5
+ import Response, { ResponseErrorType } from '../../API/Response.js'
6
+ import Transaction, { StringMap } from '../../BaseEvent/Transaction.js'
7
+ import Validator from '../../Validation/Validator.js'
8
+ import { RouterConfig } from '../Router.js'
9
+ import RouteResolver from '../RouteResolver.js'
10
10
 
11
11
  /**
12
12
  * Represents a server that handles serverless events and routes them to appropriate handlers.
@@ -1,9 +1,9 @@
1
1
  import { Request, Response } from 'express'
2
2
 
3
- import GenericHandlerEvent, { GenericHandlerEventResponse } from './GenericHandlerEvent'
4
- import Globals from '../../../Globals'
5
- import Utils from '../../../Util/Utils'
6
- import Server from '../Server'
3
+ import GenericHandlerEvent, { GenericHandlerEventResponse } from './GenericHandlerEvent.js'
4
+ import Globals from '../../../Globals.js'
5
+ import Utils from '../../../Util/Utils.js'
6
+ import Server from '../Server.js'
7
7
 
8
8
  /**
9
9
  * Creates an async function that handles serverless events and sends a response.
@@ -2,9 +2,9 @@ import type { APIGatewayProxyEvent, Context } from 'aws-lambda'
2
2
  import cuid from 'cuid'
3
3
  import { Request } from 'express'
4
4
 
5
- import { parseMultiValueQueryStringParameters, parseQueryStringParameters } from './Utils'
6
- import Globals from '../../../Globals'
7
- import Server from '../Server'
5
+ import { parseMultiValueQueryStringParameters, parseQueryStringParameters } from './Utils.js'
6
+ import Globals from '../../../Globals.js'
7
+ import Server from '../Server.js'
8
8
 
9
9
  /**
10
10
  * Represents the response object returned by a generic event handler.
@@ -4,12 +4,12 @@ import { Server as HTTPServer, createServer } from 'http'
4
4
  import cors from 'cors'
5
5
  import express from 'express'
6
6
 
7
- import Server from './../Server'
8
- import GenericHandler from './GenericHandler'
9
- import HealthHandler from './HealthHandler'
10
- import Globals from '../../../Globals'
11
- import Utils from '../../../Util/Utils'
12
- import { RouterConfig } from '../../Router'
7
+ import Server from './../Server.js'
8
+ import GenericHandler from './GenericHandler.js'
9
+ import HealthHandler from './HealthHandler.js'
10
+ import Globals from '../../../Globals.js'
11
+ import Utils from '../../../Util/Utils.js'
12
+ import { RouterConfig } from '../../Router.js'
13
13
 
14
14
  /* Get package.json version from Wapi on ESM */
15
15
  const { version: appVersion } = JSON.parse(fs.readFileSync('package.json').toString())
@@ -1,7 +1,7 @@
1
1
  import { z } from 'zod'
2
2
 
3
- import Response, { ResponseErrorType } from '../API/Response'
4
- import Globals from '../Globals'
3
+ import Response, { ResponseErrorType } from '../API/Response.js'
4
+ import Globals from '../Globals.js'
5
5
 
6
6
  /**
7
7
  * Validates the given data against the provided schema.
@@ -0,0 +1,273 @@
1
+ import { APIGatewayEvent, Context } from 'aws-lambda'
2
+ import { expect } from 'chai'
3
+
4
+ import Request, { HttpMethod } from '../../src/API/Request.js'
5
+ import Logger from '../../src/Logger/Logger.js'
6
+
7
+ function newReq(event: any, context?: any) {
8
+ const logger = new Logger(
9
+ {
10
+ logLevel: 'DEBUG',
11
+ },
12
+ '123-456'
13
+ )
14
+ return new Request<any, any, any>(
15
+ <APIGatewayEvent>(<unknown>event),
16
+ <Context>context ? context : {},
17
+ logger
18
+ )
19
+ }
20
+
21
+ describe('Request querystring', () => {
22
+ test('Null query string', () => {
23
+ const r = newReq({
24
+ queryStringParameters: null,
25
+ })
26
+ expect(r.containsQueryParam('123')).to.be.false
27
+ expect(r.getQueryParam('123')).to.be.null
28
+ })
29
+
30
+ test('Empty query string', () => {
31
+ const r = newReq({
32
+ queryStringParameters: {},
33
+ })
34
+ expect(r.containsQueryParam('123')).to.be.false
35
+ expect(r.getQueryParam('123')).to.be.null
36
+ })
37
+
38
+ test('Valid query string', () => {
39
+ const v = { '123': 'abc' }
40
+ const r = newReq({
41
+ queryStringParameters: v,
42
+ })
43
+ expect(r.containsQueryParam('123')).to.be.true
44
+ expect(r.getQueryParam('123')).to.be.equals('abc')
45
+ expect(r.getQueryParams()).to.be.deep.equal(v)
46
+ })
47
+
48
+ test('Valid query string number', () => {
49
+ const r = newReq({
50
+ queryStringParameters: { '123': 456 },
51
+ })
52
+ expect(r.containsQueryParam('123')).to.be.true
53
+ expect(r.getQueryParam('123')).to.be.equals(456)
54
+ })
55
+ })
56
+
57
+ describe('Request headers', () => {
58
+ test('Null headers', () => {
59
+ const r = newReq({ headers: null })
60
+ expect(r.getHeader('123')).to.be.null
61
+ })
62
+
63
+ test('Empty headers', () => {
64
+ const r = newReq({ headers: {} })
65
+ expect(r.getHeader('123')).to.be.null
66
+ })
67
+
68
+ test('Valid headers', () => {
69
+ const r = newReq({ headers: { '123': 'abc' } })
70
+ expect(r.getHeader('123')).to.be.equals('abc')
71
+ })
72
+
73
+ test('Auth headers', () => {
74
+ const r = newReq({ headers: { Authorization: 'abc' } })
75
+ expect(r.getAuthorizationHeader()).to.be.equals('abc')
76
+ })
77
+ })
78
+
79
+ describe('Request context', () => {
80
+ test('Null context', () => {
81
+ const r = newReq({ requestContext: null })
82
+ expect(r.getContextParam('123')).to.be.null
83
+ })
84
+
85
+ test('Empty context', () => {
86
+ const r = newReq({ requestContext: {} })
87
+ expect(r.getContextParam('123')).to.be.null
88
+ })
89
+
90
+ test('Valid context', () => {
91
+ const r = newReq({
92
+ requestContext: { '123': 'abc' },
93
+ })
94
+ expect(r.getContextParam('123')).to.be.equals('abc')
95
+ })
96
+ })
97
+
98
+ describe('Request path params', () => {
99
+ test('Null path params', () => {
100
+ const r = newReq({ pathParameters: null })
101
+ expect(r.containsPathParam('123')).to.be.false
102
+ expect(r.getPathParam('123')).to.be.null
103
+ expect(r.getPathParams()).to.be.null
104
+ })
105
+
106
+ test('Empty path params', () => {
107
+ const r = newReq({ pathParameters: {} })
108
+ expect(r.containsPathParam('123')).to.be.false
109
+ expect(r.getPathParam('123')).to.be.null
110
+ expect(r.getPathParams()).to.be.deep.equal({})
111
+ })
112
+
113
+ test('Valid path params', () => {
114
+ const v = { '123': 'abc' }
115
+ const r = newReq({
116
+ pathParameters: v,
117
+ })
118
+ expect(r.containsPathParam('123')).to.be.true
119
+ expect(r.getPathParam('123')).to.be.equals('abc')
120
+ expect(r.getPathParams()).to.be.deep.equal(v)
121
+ })
122
+
123
+ test('Valid path param number', () => {
124
+ const v = { '123': 456 }
125
+ const r = newReq({
126
+ pathParameters: v,
127
+ })
128
+ expect(r.containsPathParam('123')).to.be.true
129
+ expect(r.getPathParam('123')).to.be.equals(456)
130
+ expect(r.getPathParams()).to.be.deep.equal(v)
131
+ })
132
+
133
+ test('Fix path params', () => {
134
+ const v = { '123': 'abc' }
135
+ const r = newReq({ pathParameters: null })
136
+ r.setFixedPathParams(
137
+ Object.keys(v).map(k => ({ name: k })),
138
+ ['/'].concat(Object.values(v))
139
+ )
140
+ expect(r.containsPathParam('123')).to.be.true
141
+ expect(r.getPathParam('123')).to.be.equals('abc')
142
+ expect(r.getPathParams()).to.be.deep.equal(v)
143
+ })
144
+ })
145
+
146
+ describe('Request body', () => {
147
+ test('Null body', () => {
148
+ const r = newReq({ body: null })
149
+ expect(r.getBody()).to.be.null
150
+ })
151
+
152
+ test('Empty body', () => {
153
+ const r = newReq({ body: {} })
154
+ expect(r.getBody()).to.be.deep.equals({})
155
+ })
156
+
157
+ test('Empty string body', () => {
158
+ const r = newReq({ body: '{}' })
159
+ expect(r.getBody()).to.be.deep.equals({})
160
+ })
161
+
162
+ test('Broken json string body', () => {
163
+ const r = newReq({ body: '{name": "Joe"}' })
164
+ expect(r.getBody()).to.be.equals('{name": "Joe"}')
165
+ })
166
+
167
+ test('Valid string body', () => {
168
+ const r = newReq({ body: '{"name": "Joe"}' })
169
+ expect(r.getBody()).to.be.deep.equals({
170
+ name: 'Joe',
171
+ })
172
+ })
173
+
174
+ test('Valid json body', () => {
175
+ const r = newReq({ body: { name: 'Joe' } })
176
+ expect(r.getBody()).to.be.deep.equals({
177
+ name: 'Joe',
178
+ })
179
+ })
180
+ })
181
+
182
+ describe('Request path/method', () => {
183
+ test('Null path/method', () => {
184
+ const r = newReq({
185
+ path: null,
186
+ httpMethod: null,
187
+ })
188
+ expect(r.getPath()).to.be.null
189
+ expect(r.getMethod.bind(r)).to.throw('Invalid HTTP method: null')
190
+ })
191
+
192
+ test('Empty path/method', () => {
193
+ const r = newReq({
194
+ path: '',
195
+ httpMethod: '',
196
+ })
197
+ expect(r.getPath()).to.be.a('string')
198
+ expect(r.getPath()).to.be.equals('')
199
+ expect(r.getMethod.bind(r)).to.throw('Invalid HTTP method: ')
200
+ })
201
+
202
+ test('Valid method/path', () => {
203
+ const r = newReq({
204
+ path: '/root',
205
+ httpMethod: HttpMethod.GET,
206
+ })
207
+ expect(r.getPath()).to.be.a('string')
208
+ expect(r.getPath()).to.be.equals('/root')
209
+ expect(r.getMethod()).to.be.a('string')
210
+ expect(r.getMethod()).to.be.equals(HttpMethod.GET)
211
+ })
212
+
213
+ test('Case insensitive method', () => {
214
+ const r = newReq({
215
+ path: '/root',
216
+ httpMethod: 'poST',
217
+ })
218
+
219
+ expect(r.getMethod()).to.be.equals(HttpMethod.POST)
220
+ })
221
+ })
222
+
223
+ describe('Request ID', () => {
224
+ test('From context', () => {
225
+ const r = newReq({}, { awsRequestId: '123' })
226
+ expect(r.getRequestID()).to.not.be.null
227
+ expect(r.getRequestID()).to.be.equals('123')
228
+ })
229
+
230
+ test('From event', () => {
231
+ const r = newReq({
232
+ requestContext: { requestId: '123' },
233
+ })
234
+ expect(r.getRequestID()).to.not.be.null
235
+ expect(r.getRequestID()).to.be.equals('123')
236
+ })
237
+
238
+ test('unknown', () => {
239
+ const r = newReq({})
240
+ expect(r.getRequestID()).to.not.be.null
241
+ expect(r.getRequestID()).to.be.equals('unknown')
242
+ })
243
+ })
244
+
245
+ describe('Request origin IP', () => {
246
+ test('From context', () => {
247
+ const r = newReq({
248
+ requestContext: {
249
+ identity: { sourceIp: '127.0.0.1' },
250
+ },
251
+ })
252
+ expect(r.getOriginIP()).to.not.be.null
253
+ expect(r.getOriginIP()).to.be.equals('127.0.0.1')
254
+ })
255
+
256
+ test('From header', () => {
257
+ const r = newReq({
258
+ headers: {
259
+ 'X-Forwarded-For': '127.0.0.1',
260
+ },
261
+ })
262
+ expect(r.getOriginIP()).to.not.be.null
263
+ expect(r.getOriginIP()).to.be.equals('127.0.0.1')
264
+ })
265
+
266
+ test('unknown', () => {
267
+ const r = newReq({})
268
+ expect(r.getOriginIP()).to.not.be.null
269
+ expect(r.getOriginIP()).to.be.equals('unknown')
270
+ })
271
+ })
272
+
273
+ export {}