@dereekb/nestjs 12.7.0 → 13.0.1

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 (230) hide show
  1. package/LICENSE +1 -1
  2. package/index.cjs.default.js +1 -0
  3. package/index.cjs.js +310 -0
  4. package/index.cjs.mjs +2 -0
  5. package/index.d.ts +1 -0
  6. package/index.esm.js +288 -0
  7. package/mailgun/index.cjs.default.js +1 -0
  8. package/mailgun/index.cjs.js +639 -0
  9. package/mailgun/index.cjs.mjs +2 -0
  10. package/mailgun/index.d.ts +1 -0
  11. package/mailgun/index.esm.js +626 -0
  12. package/mailgun/package.json +24 -4
  13. package/mailgun/src/lib/mailgun.d.ts +2 -2
  14. package/mailgun/src/lib/mailgun.type.d.ts +1 -1
  15. package/mailgun/src/lib/mailgun.util.d.ts +22 -17
  16. package/openai/index.cjs.default.js +1 -0
  17. package/openai/index.cjs.js +343 -0
  18. package/openai/index.cjs.mjs +2 -0
  19. package/openai/index.d.ts +1 -0
  20. package/openai/index.esm.js +326 -0
  21. package/openai/package.json +24 -6
  22. package/openai/src/lib/index.d.ts +0 -1
  23. package/openai/src/lib/webhook/webhook.openai.controller.d.ts +1 -1
  24. package/openai/src/lib/webhook/webhook.openai.d.ts +2 -2
  25. package/openai/src/lib/webhook/webhook.openai.service.d.ts +1 -1
  26. package/package.json +43 -36
  27. package/stripe/index.cjs.default.js +1 -0
  28. package/stripe/index.cjs.js +253 -0
  29. package/stripe/index.cjs.mjs +2 -0
  30. package/stripe/index.d.ts +1 -0
  31. package/stripe/index.esm.js +245 -0
  32. package/stripe/package.json +24 -6
  33. package/stripe/src/lib/index.d.ts +1 -1
  34. package/stripe/src/lib/stripe.module.d.ts +2 -2
  35. package/stripe/src/lib/webhook/webhook.stripe.controller.d.ts +1 -1
  36. package/typeform/index.cjs.default.js +1 -0
  37. package/typeform/index.cjs.js +395 -0
  38. package/typeform/index.cjs.mjs +2 -0
  39. package/typeform/index.d.ts +1 -0
  40. package/typeform/index.esm.js +380 -0
  41. package/typeform/package.json +24 -6
  42. package/typeform/src/lib/index.d.ts +0 -1
  43. package/typeform/src/lib/webhook/webhook.typeform.controller.d.ts +1 -1
  44. package/vapiai/index.cjs.default.js +1 -0
  45. package/vapiai/index.cjs.js +319 -0
  46. package/vapiai/index.cjs.mjs +2 -0
  47. package/vapiai/index.d.ts +1 -0
  48. package/vapiai/index.esm.js +310 -0
  49. package/vapiai/package.json +24 -6
  50. package/vapiai/src/lib/index.d.ts +0 -1
  51. package/vapiai/src/lib/vapiai.type.d.ts +3 -1
  52. package/vapiai/src/lib/webhook/webhook.vapiai.controller.d.ts +1 -1
  53. package/vapiai/src/lib/webhook/webhook.vapiai.d.ts +1 -9
  54. package/vapiai/src/lib/webhook/webhook.vapiai.types.d.ts +6 -34
  55. package/CHANGELOG.md +0 -2001
  56. package/mailgun/LICENSE +0 -21
  57. package/mailgun/src/index.js +0 -5
  58. package/mailgun/src/index.js.map +0 -1
  59. package/mailgun/src/lib/index.js +0 -10
  60. package/mailgun/src/lib/index.js.map +0 -1
  61. package/mailgun/src/lib/mailgun.api.js +0 -37
  62. package/mailgun/src/lib/mailgun.api.js.map +0 -1
  63. package/mailgun/src/lib/mailgun.config.js +0 -45
  64. package/mailgun/src/lib/mailgun.config.js.map +0 -1
  65. package/mailgun/src/lib/mailgun.js +0 -205
  66. package/mailgun/src/lib/mailgun.js.map +0 -1
  67. package/mailgun/src/lib/mailgun.service.js +0 -53
  68. package/mailgun/src/lib/mailgun.service.js.map +0 -1
  69. package/mailgun/src/lib/mailgun.service.module.js +0 -84
  70. package/mailgun/src/lib/mailgun.service.module.js.map +0 -1
  71. package/mailgun/src/lib/mailgun.type.js +0 -3
  72. package/mailgun/src/lib/mailgun.type.js.map +0 -1
  73. package/mailgun/src/lib/mailgun.util.js +0 -206
  74. package/mailgun/src/lib/mailgun.util.js.map +0 -1
  75. package/openai/LICENSE +0 -21
  76. package/openai/src/index.js +0 -5
  77. package/openai/src/index.js.map +0 -1
  78. package/openai/src/lib/index.js +0 -11
  79. package/openai/src/lib/index.js.map +0 -1
  80. package/openai/src/lib/openai.api.js +0 -22
  81. package/openai/src/lib/openai.api.js.map +0 -1
  82. package/openai/src/lib/openai.config.js +0 -29
  83. package/openai/src/lib/openai.config.js.map +0 -1
  84. package/openai/src/lib/openai.module.js +0 -41
  85. package/openai/src/lib/openai.module.js.map +0 -1
  86. package/openai/src/lib/openai.type.js +0 -3
  87. package/openai/src/lib/openai.type.js.map +0 -1
  88. package/openai/src/lib/openai.util.js +0 -48
  89. package/openai/src/lib/openai.util.js.map +0 -1
  90. package/openai/src/lib/webhook/index.js +0 -10
  91. package/openai/src/lib/webhook/index.js.map +0 -1
  92. package/openai/src/lib/webhook/webhook.openai.config.js +0 -17
  93. package/openai/src/lib/webhook/webhook.openai.config.js.map +0 -1
  94. package/openai/src/lib/webhook/webhook.openai.controller.js +0 -30
  95. package/openai/src/lib/webhook/webhook.openai.controller.js.map +0 -1
  96. package/openai/src/lib/webhook/webhook.openai.js +0 -41
  97. package/openai/src/lib/webhook/webhook.openai.js.map +0 -1
  98. package/openai/src/lib/webhook/webhook.openai.module.js +0 -39
  99. package/openai/src/lib/webhook/webhook.openai.module.js.map +0 -1
  100. package/openai/src/lib/webhook/webhook.openai.service.js +0 -48
  101. package/openai/src/lib/webhook/webhook.openai.service.js.map +0 -1
  102. package/openai/src/lib/webhook/webhook.openai.verify.js +0 -37
  103. package/openai/src/lib/webhook/webhook.openai.verify.js.map +0 -1
  104. package/src/index.js +0 -5
  105. package/src/index.js.map +0 -1
  106. package/src/lib/decorators/index.js +0 -6
  107. package/src/lib/decorators/index.js.map +0 -1
  108. package/src/lib/decorators/local.decorator.js +0 -17
  109. package/src/lib/decorators/local.decorator.js.map +0 -1
  110. package/src/lib/decorators/rawbody.js +0 -44
  111. package/src/lib/decorators/rawbody.js.map +0 -1
  112. package/src/lib/index.js +0 -7
  113. package/src/lib/index.js.map +0 -1
  114. package/src/lib/middlewares/index.js +0 -7
  115. package/src/lib/middlewares/index.js.map +0 -1
  116. package/src/lib/middlewares/json.middleware.js +0 -16
  117. package/src/lib/middlewares/json.middleware.js.map +0 -1
  118. package/src/lib/middlewares/rawbody.middleware.js +0 -16
  119. package/src/lib/middlewares/rawbody.middleware.js.map +0 -1
  120. package/src/lib/middlewares/webhook.js +0 -51
  121. package/src/lib/middlewares/webhook.js.map +0 -1
  122. package/src/lib/module/client/client.config.js +0 -13
  123. package/src/lib/module/client/client.config.js.map +0 -1
  124. package/src/lib/module/client/client.js +0 -8
  125. package/src/lib/module/client/client.js.map +0 -1
  126. package/src/lib/module/client/client.module.js +0 -36
  127. package/src/lib/module/client/client.module.js.map +0 -1
  128. package/src/lib/module/client/client.service.js +0 -30
  129. package/src/lib/module/client/client.service.js.map +0 -1
  130. package/src/lib/module/client/index.js +0 -8
  131. package/src/lib/module/client/index.js.map +0 -1
  132. package/src/lib/module/env/env.config.js +0 -14
  133. package/src/lib/module/env/env.config.js.map +0 -1
  134. package/src/lib/module/env/env.js +0 -10
  135. package/src/lib/module/env/env.js.map +0 -1
  136. package/src/lib/module/env/env.nest.js +0 -16
  137. package/src/lib/module/env/env.nest.js.map +0 -1
  138. package/src/lib/module/env/env.service.js +0 -33
  139. package/src/lib/module/env/env.service.js.map +0 -1
  140. package/src/lib/module/env/index.js +0 -8
  141. package/src/lib/module/env/index.js.map +0 -1
  142. package/src/lib/module/index.js +0 -7
  143. package/src/lib/module/index.js.map +0 -1
  144. package/src/lib/module/module.js +0 -26
  145. package/src/lib/module/module.js.map +0 -1
  146. package/stripe/LICENSE +0 -21
  147. package/stripe/src/index.js +0 -5
  148. package/stripe/src/index.js.map +0 -1
  149. package/stripe/src/lib/index.js +0 -8
  150. package/stripe/src/lib/index.js.map +0 -1
  151. package/stripe/src/lib/stripe.api.js +0 -40
  152. package/stripe/src/lib/stripe.api.js.map +0 -1
  153. package/stripe/src/lib/stripe.config.js +0 -19
  154. package/stripe/src/lib/stripe.config.js.map +0 -1
  155. package/stripe/src/lib/stripe.module.js +0 -41
  156. package/stripe/src/lib/stripe.module.js.map +0 -1
  157. package/stripe/src/lib/stripe.type.js +0 -4
  158. package/stripe/src/lib/stripe.type.js.map +0 -1
  159. package/stripe/src/lib/webhook/index.js +0 -8
  160. package/stripe/src/lib/webhook/index.js.map +0 -1
  161. package/stripe/src/lib/webhook/webhook.stripe.controller.js +0 -30
  162. package/stripe/src/lib/webhook/webhook.stripe.controller.js.map +0 -1
  163. package/stripe/src/lib/webhook/webhook.stripe.js +0 -67
  164. package/stripe/src/lib/webhook/webhook.stripe.js.map +0 -1
  165. package/stripe/src/lib/webhook/webhook.stripe.module.js +0 -20
  166. package/stripe/src/lib/webhook/webhook.stripe.module.js.map +0 -1
  167. package/stripe/src/lib/webhook/webhook.stripe.service.js +0 -36
  168. package/stripe/src/lib/webhook/webhook.stripe.service.js.map +0 -1
  169. package/typeform/LICENSE +0 -21
  170. package/typeform/src/index.js +0 -5
  171. package/typeform/src/index.js.map +0 -1
  172. package/typeform/src/lib/index.js +0 -11
  173. package/typeform/src/lib/index.js.map +0 -1
  174. package/typeform/src/lib/typeform.api.js +0 -22
  175. package/typeform/src/lib/typeform.api.js.map +0 -1
  176. package/typeform/src/lib/typeform.config.js +0 -21
  177. package/typeform/src/lib/typeform.config.js.map +0 -1
  178. package/typeform/src/lib/typeform.module.js +0 -39
  179. package/typeform/src/lib/typeform.module.js.map +0 -1
  180. package/typeform/src/lib/typeform.type.js +0 -3
  181. package/typeform/src/lib/typeform.type.js.map +0 -1
  182. package/typeform/src/lib/typeform.util.js +0 -26
  183. package/typeform/src/lib/typeform.util.js.map +0 -1
  184. package/typeform/src/lib/webhook/index.js +0 -12
  185. package/typeform/src/lib/webhook/index.js.map +0 -1
  186. package/typeform/src/lib/webhook/webhook.typeform.config.js +0 -17
  187. package/typeform/src/lib/webhook/webhook.typeform.config.js.map +0 -1
  188. package/typeform/src/lib/webhook/webhook.typeform.controller.js +0 -30
  189. package/typeform/src/lib/webhook/webhook.typeform.controller.js.map +0 -1
  190. package/typeform/src/lib/webhook/webhook.typeform.form.js +0 -113
  191. package/typeform/src/lib/webhook/webhook.typeform.form.js.map +0 -1
  192. package/typeform/src/lib/webhook/webhook.typeform.js +0 -27
  193. package/typeform/src/lib/webhook/webhook.typeform.js.map +0 -1
  194. package/typeform/src/lib/webhook/webhook.typeform.module.js +0 -38
  195. package/typeform/src/lib/webhook/webhook.typeform.module.js.map +0 -1
  196. package/typeform/src/lib/webhook/webhook.typeform.service.js +0 -45
  197. package/typeform/src/lib/webhook/webhook.typeform.service.js.map +0 -1
  198. package/typeform/src/lib/webhook/webhook.typeform.type.js +0 -3
  199. package/typeform/src/lib/webhook/webhook.typeform.type.js.map +0 -1
  200. package/typeform/src/lib/webhook/webhook.typeform.verify.js +0 -28
  201. package/typeform/src/lib/webhook/webhook.typeform.verify.js.map +0 -1
  202. package/vapiai/LICENSE +0 -21
  203. package/vapiai/src/index.js +0 -5
  204. package/vapiai/src/index.js.map +0 -1
  205. package/vapiai/src/lib/index.js +0 -10
  206. package/vapiai/src/lib/index.js.map +0 -1
  207. package/vapiai/src/lib/vapiai.api.js +0 -32
  208. package/vapiai/src/lib/vapiai.api.js.map +0 -1
  209. package/vapiai/src/lib/vapiai.config.js +0 -17
  210. package/vapiai/src/lib/vapiai.config.js.map +0 -1
  211. package/vapiai/src/lib/vapiai.module.js +0 -38
  212. package/vapiai/src/lib/vapiai.module.js.map +0 -1
  213. package/vapiai/src/lib/vapiai.type.js +0 -3
  214. package/vapiai/src/lib/vapiai.type.js.map +0 -1
  215. package/vapiai/src/lib/webhook/index.js +0 -10
  216. package/vapiai/src/lib/webhook/index.js.map +0 -1
  217. package/vapiai/src/lib/webhook/webhook.vapi.config.js +0 -20
  218. package/vapiai/src/lib/webhook/webhook.vapi.config.js.map +0 -1
  219. package/vapiai/src/lib/webhook/webhook.vapiai.controller.js +0 -38
  220. package/vapiai/src/lib/webhook/webhook.vapiai.controller.js.map +0 -1
  221. package/vapiai/src/lib/webhook/webhook.vapiai.js +0 -42
  222. package/vapiai/src/lib/webhook/webhook.vapiai.js.map +0 -1
  223. package/vapiai/src/lib/webhook/webhook.vapiai.module.js +0 -41
  224. package/vapiai/src/lib/webhook/webhook.vapiai.module.js.map +0 -1
  225. package/vapiai/src/lib/webhook/webhook.vapiai.service.js +0 -52
  226. package/vapiai/src/lib/webhook/webhook.vapiai.service.js.map +0 -1
  227. package/vapiai/src/lib/webhook/webhook.vapiai.types.js +0 -25
  228. package/vapiai/src/lib/webhook/webhook.vapiai.types.js.map +0 -1
  229. package/vapiai/src/lib/webhook/webhook.vapiai.verify.js +0 -49
  230. package/vapiai/src/lib/webhook/webhook.vapiai.verify.js.map +0 -1
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 Hapier Creative LLC.
3
+ Copyright (c) 2025 Hapier Creative LLC.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -0,0 +1 @@
1
+ exports._default = require('./index.cjs.js').default;
package/index.cjs.js ADDED
@@ -0,0 +1,310 @@
1
+ 'use strict';
2
+
3
+ var common = require('@nestjs/common');
4
+ var rawbody = require('raw-body');
5
+ var querystring = require('querystring');
6
+ var bodyParser = require('body-parser');
7
+ var util = require('@dereekb/util');
8
+ var config = require('@nestjs/config');
9
+
10
+ /**
11
+ * Returns true if the request is from localhost:4200.
12
+ */
13
+ const IsRequestFromLocalHost = common.createParamDecorator((data, context) => {
14
+ return isLocalhost(context);
15
+ });
16
+ function isLocalhost(context) {
17
+ const req = context.switchToHttp().getRequest();
18
+ const origin = req.headers['origin'] ?? '';
19
+ return origin.startsWith('http://localhost') || origin.startsWith('https://localhost');
20
+ }
21
+
22
+ const ParseRawBody = common.createParamDecorator(async (_, context) => {
23
+ const req = context.switchToHttp().getRequest();
24
+ if (!req.readable) {
25
+ console.error('RawBody request was not readable. This is generally due to bad configuration.');
26
+ throw new common.BadRequestException('Invalid body');
27
+ }
28
+ const body = await rawbody(req);
29
+ return body;
30
+ });
31
+ const RawBody = common.createParamDecorator(async (_, context) => {
32
+ const req = context.switchToHttp().getRequest();
33
+ const body = req.body;
34
+ if (!Buffer.isBuffer(body)) {
35
+ console.error('RawBody expected a buffer set to req.body.');
36
+ throw new common.InternalServerErrorException('failed parsing body');
37
+ }
38
+ return body;
39
+ });
40
+ const ParsedQueryRawBody = common.createParamDecorator(async (_, context) => {
41
+ const req = context.switchToHttp().getRequest();
42
+ req.body = RawBodyToParsedQueryString(req.body);
43
+ return req.body;
44
+ });
45
+ function RawBodyToJson(rawBody) {
46
+ const string = RawBodyToString(rawBody);
47
+ return JSON.parse(string);
48
+ }
49
+ function RawBodyToParsedQueryString(rawBody) {
50
+ const string = RawBodyToString(rawBody);
51
+ return querystring.parse(string);
52
+ }
53
+ function RawBodyToString(rawBody) {
54
+ return rawBody.toString('utf8').trim();
55
+ }
56
+
57
+ /******************************************************************************
58
+ Copyright (c) Microsoft Corporation.
59
+
60
+ Permission to use, copy, modify, and/or distribute this software for any
61
+ purpose with or without fee is hereby granted.
62
+
63
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
64
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
65
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
66
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
67
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
68
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
69
+ PERFORMANCE OF THIS SOFTWARE.
70
+ ***************************************************************************** */
71
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
72
+
73
+
74
+ function __decorate(decorators, target, key, desc) {
75
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
76
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
77
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
78
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
79
+ }
80
+
81
+ function __param(paramIndex, decorator) {
82
+ return function (target, key) { decorator(target, key, paramIndex); }
83
+ }
84
+
85
+ function __metadata(metadataKey, metadataValue) {
86
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
87
+ }
88
+
89
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
90
+ var e = new Error(message);
91
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
92
+ };
93
+
94
+ exports.JsonBodyMiddleware = class JsonBodyMiddleware {
95
+ use(req, res, next) {
96
+ bodyParser.json()(req, res, next);
97
+ }
98
+ };
99
+ exports.JsonBodyMiddleware = __decorate([
100
+ common.Injectable()
101
+ ], exports.JsonBodyMiddleware);
102
+
103
+ exports.RawBodyMiddleware = class RawBodyMiddleware {
104
+ use(req, res, next) {
105
+ bodyParser.raw({ type: '*/*' })(req, res, next);
106
+ }
107
+ };
108
+ exports.RawBodyMiddleware = __decorate([
109
+ common.Injectable()
110
+ ], exports.RawBodyMiddleware);
111
+
112
+ const DEFAULT_BASE_WEBHOOK_PATH = '/webhook';
113
+ const DEFAULT_WEBHOOK_MIDDLEWARE_ROUTE_INFO = {
114
+ path: `${DEFAULT_BASE_WEBHOOK_PATH}/*`,
115
+ method: common.RequestMethod.POST
116
+ };
117
+ /**
118
+ * Convenience class that configures a nestjs module (typically the root app module) to apply the proper middleware for handling webhooks.
119
+ */
120
+ class AppModuleWithWebhooksEnabled {
121
+ configure(consumer) {
122
+ consumeWebhooksWithRawBodyMiddleware(consumer);
123
+ }
124
+ }
125
+ /**
126
+ * Convenience class that extends AppWithWebhooksEnabled.
127
+ */
128
+ exports.ConfigureWebhookMiddlewareModule = class ConfigureWebhookMiddlewareModule extends AppModuleWithWebhooksEnabled {
129
+ logger = new common.Logger('ConfigureWebhookMiddlewareModule');
130
+ configure(consumer) {
131
+ super.configure(consumer);
132
+ this.logger.debug('Configured webhook routes with proper middleware.');
133
+ }
134
+ };
135
+ exports.ConfigureWebhookMiddlewareModule = __decorate([
136
+ common.Module({})
137
+ ], exports.ConfigureWebhookMiddlewareModule);
138
+ /**
139
+ * Configures a MiddlewareConsumer to use RawBodyMiddleware for all POST requests to /webhook/*. All other routes are consumed with the JsonBodyMiddleware.
140
+ *
141
+ * This is required for various webhooks that require the full body to properly parse content.
142
+ *
143
+ * Be sure to also set bodyParsing: false in the nest app options.
144
+ *
145
+ * @param consumer
146
+ */
147
+ function consumeWebhooksWithRawBodyMiddleware(consumer) {
148
+ // Configure the app to not parse the body for our webhook routes.
149
+ // https://stackoverflow.com/questions/54346465/access-raw-body-of-stripe-webhook-in-nest-js
150
+ consumer.apply(exports.RawBodyMiddleware).forRoutes(DEFAULT_WEBHOOK_MIDDLEWARE_ROUTE_INFO).apply(exports.JsonBodyMiddleware).forRoutes('*');
151
+ }
152
+
153
+ /**
154
+ * Merges two module metadata entries together.
155
+ *
156
+ * @param base
157
+ * @param additional
158
+ * @returns
159
+ */
160
+ function mergeModuleMetadata(base, additional = {}) {
161
+ return {
162
+ controllers: util.mergeArrays([base.controllers, additional.controllers]),
163
+ imports: util.mergeArrays([base.imports, additional.imports]),
164
+ exports: util.mergeArrays([base.exports, additional.exports]),
165
+ providers: util.mergeArrays([base.providers, additional.providers])
166
+ };
167
+ }
168
+ function injectionTokensFromProviders(providers) {
169
+ return util.asArray(providers).map((x) => {
170
+ return typeof x === 'object' ? x.provide : x;
171
+ });
172
+ }
173
+
174
+ /**
175
+ * Environment variable that specfies the ClientWebAppUrl for your application
176
+ */
177
+ const CLIENT_WEB_APP_URL_ENV_VAR = 'CLIENT_WEB_APP_URL';
178
+
179
+ class ClientAppServiceConfig {
180
+ client;
181
+ static assertValidConfig(config) {
182
+ if (!config.client.clientWebAppUrl) {
183
+ throw new Error('No client app url specified.');
184
+ }
185
+ }
186
+ }
187
+
188
+ /**
189
+ * Provides information about companion apps and websites for the project.
190
+ */
191
+ exports.ClientAppService = class ClientAppService {
192
+ _config;
193
+ constructor(config) {
194
+ this._config = config;
195
+ }
196
+ get config() {
197
+ return this._config;
198
+ }
199
+ get webAppUrl() {
200
+ return this.config.client.clientWebAppUrl;
201
+ }
202
+ get webAppHost() {
203
+ return this.webAppUrl.split('://', 2)[1];
204
+ }
205
+ };
206
+ exports.ClientAppService = __decorate([
207
+ common.Injectable(),
208
+ __param(0, common.Inject(ClientAppServiceConfig)),
209
+ __metadata("design:paramtypes", [ClientAppServiceConfig])
210
+ ], exports.ClientAppService);
211
+
212
+ function clientAppConfigFactory(configService) {
213
+ const config = {
214
+ client: {
215
+ clientWebAppUrl: configService.get(CLIENT_WEB_APP_URL_ENV_VAR)
216
+ }
217
+ };
218
+ ClientAppServiceConfig.assertValidConfig(config);
219
+ return config;
220
+ }
221
+ exports.ClientAppModule = class ClientAppModule {
222
+ };
223
+ exports.ClientAppModule = __decorate([
224
+ common.Module({
225
+ imports: [config.ConfigModule],
226
+ providers: [
227
+ {
228
+ provide: ClientAppServiceConfig,
229
+ inject: [config.ConfigService],
230
+ useFactory: clientAppConfigFactory
231
+ }
232
+ ],
233
+ exports: [exports.ClientAppService]
234
+ })
235
+ ], exports.ClientAppModule);
236
+
237
+ /**
238
+ * A server environment configuration.
239
+ *
240
+ * Explicitly states whether or not this is a production environment, and optionally some other config.
241
+ *
242
+ * This config is not meant to replace other typical configurations, like .env files, but instead is part of the build system.
243
+ */
244
+ class ServerEnvironmentConfig {
245
+ }
246
+
247
+ // MARK: Tokens
248
+ /**
249
+ * Token to access a configured ServerEnvironmentServiceConfig for the app.
250
+ */
251
+ const SERVER_ENV_TOKEN = 'SERVER_ENV_TOKEN';
252
+ function serverEnvTokenProvider(env) {
253
+ return {
254
+ provide: SERVER_ENV_TOKEN,
255
+ useValue: env
256
+ };
257
+ }
258
+
259
+ /**
260
+ * Checks process.env.NODE_ENV for "test"
261
+ */
262
+ function isTestNodeEnv() {
263
+ return process.env['NODE_ENV'] === 'test';
264
+ }
265
+
266
+ exports.ServerEnvironmentService = class ServerEnvironmentService {
267
+ env;
268
+ constructor(env) {
269
+ this.env = env;
270
+ }
271
+ get isTestingEnv() {
272
+ return isTestNodeEnv();
273
+ }
274
+ get isProduction() {
275
+ return this.env.production;
276
+ }
277
+ get isStaging() {
278
+ return Boolean(this.env.staging);
279
+ }
280
+ get developerToolsEnabled() {
281
+ return Boolean(!this.isProduction && this.env.developerToolsEnabled);
282
+ }
283
+ };
284
+ exports.ServerEnvironmentService = __decorate([
285
+ common.Injectable(),
286
+ __param(0, common.Inject(SERVER_ENV_TOKEN)),
287
+ __metadata("design:paramtypes", [ServerEnvironmentConfig])
288
+ ], exports.ServerEnvironmentService);
289
+
290
+ exports.AppModuleWithWebhooksEnabled = AppModuleWithWebhooksEnabled;
291
+ exports.CLIENT_WEB_APP_URL_ENV_VAR = CLIENT_WEB_APP_URL_ENV_VAR;
292
+ exports.ClientAppServiceConfig = ClientAppServiceConfig;
293
+ exports.DEFAULT_BASE_WEBHOOK_PATH = DEFAULT_BASE_WEBHOOK_PATH;
294
+ exports.DEFAULT_WEBHOOK_MIDDLEWARE_ROUTE_INFO = DEFAULT_WEBHOOK_MIDDLEWARE_ROUTE_INFO;
295
+ exports.IsRequestFromLocalHost = IsRequestFromLocalHost;
296
+ exports.ParseRawBody = ParseRawBody;
297
+ exports.ParsedQueryRawBody = ParsedQueryRawBody;
298
+ exports.RawBody = RawBody;
299
+ exports.RawBodyToJson = RawBodyToJson;
300
+ exports.RawBodyToParsedQueryString = RawBodyToParsedQueryString;
301
+ exports.RawBodyToString = RawBodyToString;
302
+ exports.SERVER_ENV_TOKEN = SERVER_ENV_TOKEN;
303
+ exports.ServerEnvironmentConfig = ServerEnvironmentConfig;
304
+ exports.clientAppConfigFactory = clientAppConfigFactory;
305
+ exports.consumeWebhooksWithRawBodyMiddleware = consumeWebhooksWithRawBodyMiddleware;
306
+ exports.injectionTokensFromProviders = injectionTokensFromProviders;
307
+ exports.isLocalhost = isLocalhost;
308
+ exports.isTestNodeEnv = isTestNodeEnv;
309
+ exports.mergeModuleMetadata = mergeModuleMetadata;
310
+ exports.serverEnvTokenProvider = serverEnvTokenProvider;
package/index.cjs.mjs ADDED
@@ -0,0 +1,2 @@
1
+ export * from './index.cjs.js';
2
+ export { _default as default } from './index.cjs.default.js';
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index";
package/index.esm.js ADDED
@@ -0,0 +1,288 @@
1
+ import { createParamDecorator, BadRequestException, InternalServerErrorException, Injectable, RequestMethod, Module, Logger, Inject } from '@nestjs/common';
2
+ import rawbody from 'raw-body';
3
+ import { parse } from 'querystring';
4
+ import bodyParser from 'body-parser';
5
+ import { asArray, mergeArrays } from '@dereekb/util';
6
+ import { ConfigService, ConfigModule } from '@nestjs/config';
7
+
8
+ /**
9
+ * Returns true if the request is from localhost:4200.
10
+ */
11
+ const IsRequestFromLocalHost = createParamDecorator((data, context) => {
12
+ return isLocalhost(context);
13
+ });
14
+ function isLocalhost(context) {
15
+ const req = context.switchToHttp().getRequest();
16
+ const origin = req.headers['origin'] ?? '';
17
+ return origin.startsWith('http://localhost') || origin.startsWith('https://localhost');
18
+ }
19
+
20
+ const ParseRawBody = createParamDecorator(async (_, context) => {
21
+ const req = context.switchToHttp().getRequest();
22
+ if (!req.readable) {
23
+ console.error('RawBody request was not readable. This is generally due to bad configuration.');
24
+ throw new BadRequestException('Invalid body');
25
+ }
26
+ const body = await rawbody(req);
27
+ return body;
28
+ });
29
+ const RawBody = createParamDecorator(async (_, context) => {
30
+ const req = context.switchToHttp().getRequest();
31
+ const body = req.body;
32
+ if (!Buffer.isBuffer(body)) {
33
+ console.error('RawBody expected a buffer set to req.body.');
34
+ throw new InternalServerErrorException('failed parsing body');
35
+ }
36
+ return body;
37
+ });
38
+ const ParsedQueryRawBody = createParamDecorator(async (_, context) => {
39
+ const req = context.switchToHttp().getRequest();
40
+ req.body = RawBodyToParsedQueryString(req.body);
41
+ return req.body;
42
+ });
43
+ function RawBodyToJson(rawBody) {
44
+ const string = RawBodyToString(rawBody);
45
+ return JSON.parse(string);
46
+ }
47
+ function RawBodyToParsedQueryString(rawBody) {
48
+ const string = RawBodyToString(rawBody);
49
+ return parse(string);
50
+ }
51
+ function RawBodyToString(rawBody) {
52
+ return rawBody.toString('utf8').trim();
53
+ }
54
+
55
+ /******************************************************************************
56
+ Copyright (c) Microsoft Corporation.
57
+
58
+ Permission to use, copy, modify, and/or distribute this software for any
59
+ purpose with or without fee is hereby granted.
60
+
61
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
62
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
63
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
64
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
65
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
66
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
67
+ PERFORMANCE OF THIS SOFTWARE.
68
+ ***************************************************************************** */
69
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
70
+
71
+
72
+ function __decorate(decorators, target, key, desc) {
73
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
74
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
75
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
76
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
77
+ }
78
+
79
+ function __param(paramIndex, decorator) {
80
+ return function (target, key) { decorator(target, key, paramIndex); }
81
+ }
82
+
83
+ function __metadata(metadataKey, metadataValue) {
84
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
85
+ }
86
+
87
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
88
+ var e = new Error(message);
89
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
90
+ };
91
+
92
+ let JsonBodyMiddleware = class JsonBodyMiddleware {
93
+ use(req, res, next) {
94
+ bodyParser.json()(req, res, next);
95
+ }
96
+ };
97
+ JsonBodyMiddleware = __decorate([
98
+ Injectable()
99
+ ], JsonBodyMiddleware);
100
+
101
+ let RawBodyMiddleware = class RawBodyMiddleware {
102
+ use(req, res, next) {
103
+ bodyParser.raw({ type: '*/*' })(req, res, next);
104
+ }
105
+ };
106
+ RawBodyMiddleware = __decorate([
107
+ Injectable()
108
+ ], RawBodyMiddleware);
109
+
110
+ const DEFAULT_BASE_WEBHOOK_PATH = '/webhook';
111
+ const DEFAULT_WEBHOOK_MIDDLEWARE_ROUTE_INFO = {
112
+ path: `${DEFAULT_BASE_WEBHOOK_PATH}/*`,
113
+ method: RequestMethod.POST
114
+ };
115
+ /**
116
+ * Convenience class that configures a nestjs module (typically the root app module) to apply the proper middleware for handling webhooks.
117
+ */
118
+ class AppModuleWithWebhooksEnabled {
119
+ configure(consumer) {
120
+ consumeWebhooksWithRawBodyMiddleware(consumer);
121
+ }
122
+ }
123
+ /**
124
+ * Convenience class that extends AppWithWebhooksEnabled.
125
+ */
126
+ let ConfigureWebhookMiddlewareModule = class ConfigureWebhookMiddlewareModule extends AppModuleWithWebhooksEnabled {
127
+ logger = new Logger('ConfigureWebhookMiddlewareModule');
128
+ configure(consumer) {
129
+ super.configure(consumer);
130
+ this.logger.debug('Configured webhook routes with proper middleware.');
131
+ }
132
+ };
133
+ ConfigureWebhookMiddlewareModule = __decorate([
134
+ Module({})
135
+ ], ConfigureWebhookMiddlewareModule);
136
+ /**
137
+ * Configures a MiddlewareConsumer to use RawBodyMiddleware for all POST requests to /webhook/*. All other routes are consumed with the JsonBodyMiddleware.
138
+ *
139
+ * This is required for various webhooks that require the full body to properly parse content.
140
+ *
141
+ * Be sure to also set bodyParsing: false in the nest app options.
142
+ *
143
+ * @param consumer
144
+ */
145
+ function consumeWebhooksWithRawBodyMiddleware(consumer) {
146
+ // Configure the app to not parse the body for our webhook routes.
147
+ // https://stackoverflow.com/questions/54346465/access-raw-body-of-stripe-webhook-in-nest-js
148
+ consumer.apply(RawBodyMiddleware).forRoutes(DEFAULT_WEBHOOK_MIDDLEWARE_ROUTE_INFO).apply(JsonBodyMiddleware).forRoutes('*');
149
+ }
150
+
151
+ /**
152
+ * Merges two module metadata entries together.
153
+ *
154
+ * @param base
155
+ * @param additional
156
+ * @returns
157
+ */
158
+ function mergeModuleMetadata(base, additional = {}) {
159
+ return {
160
+ controllers: mergeArrays([base.controllers, additional.controllers]),
161
+ imports: mergeArrays([base.imports, additional.imports]),
162
+ exports: mergeArrays([base.exports, additional.exports]),
163
+ providers: mergeArrays([base.providers, additional.providers])
164
+ };
165
+ }
166
+ function injectionTokensFromProviders(providers) {
167
+ return asArray(providers).map((x) => {
168
+ return typeof x === 'object' ? x.provide : x;
169
+ });
170
+ }
171
+
172
+ /**
173
+ * Environment variable that specfies the ClientWebAppUrl for your application
174
+ */
175
+ const CLIENT_WEB_APP_URL_ENV_VAR = 'CLIENT_WEB_APP_URL';
176
+
177
+ class ClientAppServiceConfig {
178
+ client;
179
+ static assertValidConfig(config) {
180
+ if (!config.client.clientWebAppUrl) {
181
+ throw new Error('No client app url specified.');
182
+ }
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Provides information about companion apps and websites for the project.
188
+ */
189
+ let ClientAppService = class ClientAppService {
190
+ _config;
191
+ constructor(config) {
192
+ this._config = config;
193
+ }
194
+ get config() {
195
+ return this._config;
196
+ }
197
+ get webAppUrl() {
198
+ return this.config.client.clientWebAppUrl;
199
+ }
200
+ get webAppHost() {
201
+ return this.webAppUrl.split('://', 2)[1];
202
+ }
203
+ };
204
+ ClientAppService = __decorate([
205
+ Injectable(),
206
+ __param(0, Inject(ClientAppServiceConfig)),
207
+ __metadata("design:paramtypes", [ClientAppServiceConfig])
208
+ ], ClientAppService);
209
+
210
+ function clientAppConfigFactory(configService) {
211
+ const config = {
212
+ client: {
213
+ clientWebAppUrl: configService.get(CLIENT_WEB_APP_URL_ENV_VAR)
214
+ }
215
+ };
216
+ ClientAppServiceConfig.assertValidConfig(config);
217
+ return config;
218
+ }
219
+ let ClientAppModule = class ClientAppModule {
220
+ };
221
+ ClientAppModule = __decorate([
222
+ Module({
223
+ imports: [ConfigModule],
224
+ providers: [
225
+ {
226
+ provide: ClientAppServiceConfig,
227
+ inject: [ConfigService],
228
+ useFactory: clientAppConfigFactory
229
+ }
230
+ ],
231
+ exports: [ClientAppService]
232
+ })
233
+ ], ClientAppModule);
234
+
235
+ /**
236
+ * A server environment configuration.
237
+ *
238
+ * Explicitly states whether or not this is a production environment, and optionally some other config.
239
+ *
240
+ * This config is not meant to replace other typical configurations, like .env files, but instead is part of the build system.
241
+ */
242
+ class ServerEnvironmentConfig {
243
+ }
244
+
245
+ // MARK: Tokens
246
+ /**
247
+ * Token to access a configured ServerEnvironmentServiceConfig for the app.
248
+ */
249
+ const SERVER_ENV_TOKEN = 'SERVER_ENV_TOKEN';
250
+ function serverEnvTokenProvider(env) {
251
+ return {
252
+ provide: SERVER_ENV_TOKEN,
253
+ useValue: env
254
+ };
255
+ }
256
+
257
+ /**
258
+ * Checks process.env.NODE_ENV for "test"
259
+ */
260
+ function isTestNodeEnv() {
261
+ return process.env['NODE_ENV'] === 'test';
262
+ }
263
+
264
+ let ServerEnvironmentService = class ServerEnvironmentService {
265
+ env;
266
+ constructor(env) {
267
+ this.env = env;
268
+ }
269
+ get isTestingEnv() {
270
+ return isTestNodeEnv();
271
+ }
272
+ get isProduction() {
273
+ return this.env.production;
274
+ }
275
+ get isStaging() {
276
+ return Boolean(this.env.staging);
277
+ }
278
+ get developerToolsEnabled() {
279
+ return Boolean(!this.isProduction && this.env.developerToolsEnabled);
280
+ }
281
+ };
282
+ ServerEnvironmentService = __decorate([
283
+ Injectable(),
284
+ __param(0, Inject(SERVER_ENV_TOKEN)),
285
+ __metadata("design:paramtypes", [ServerEnvironmentConfig])
286
+ ], ServerEnvironmentService);
287
+
288
+ export { AppModuleWithWebhooksEnabled, CLIENT_WEB_APP_URL_ENV_VAR, ClientAppModule, ClientAppService, ClientAppServiceConfig, ConfigureWebhookMiddlewareModule, DEFAULT_BASE_WEBHOOK_PATH, DEFAULT_WEBHOOK_MIDDLEWARE_ROUTE_INFO, IsRequestFromLocalHost, JsonBodyMiddleware, ParseRawBody, ParsedQueryRawBody, RawBody, RawBodyMiddleware, RawBodyToJson, RawBodyToParsedQueryString, RawBodyToString, SERVER_ENV_TOKEN, ServerEnvironmentConfig, ServerEnvironmentService, clientAppConfigFactory, consumeWebhooksWithRawBodyMiddleware, injectionTokensFromProviders, isLocalhost, isTestNodeEnv, mergeModuleMetadata, serverEnvTokenProvider };
@@ -0,0 +1 @@
1
+ exports._default = require('./index.cjs.js').default;