arkos 1.4.0-canary.6 → 1.4.0-canary.8
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/dist/cjs/exports/index.js.map +1 -1
- package/dist/cjs/modules/auth/auth.controller.js +22 -15
- package/dist/cjs/modules/auth/auth.controller.js.map +1 -1
- package/dist/cjs/modules/base/base.controller.js +3 -0
- package/dist/cjs/modules/base/base.controller.js.map +1 -1
- package/dist/cjs/modules/base/base.middlewares.js +73 -16
- package/dist/cjs/modules/base/base.middlewares.js.map +1 -1
- package/dist/cjs/modules/email/email.service.js +24 -27
- package/dist/cjs/modules/email/email.service.js.map +1 -1
- package/dist/cjs/modules/file-upload/file-upload.controller.js +4 -0
- package/dist/cjs/modules/file-upload/file-upload.controller.js.map +1 -1
- package/dist/cjs/utils/arkos-router/index.js +11 -6
- package/dist/cjs/utils/arkos-router/index.js.map +1 -1
- package/dist/cjs/utils/arkos-router/types/index.js.map +1 -1
- package/dist/cjs/utils/arkos-router/utils/helpers/index.js +1 -2
- package/dist/cjs/utils/arkos-router/utils/helpers/index.js.map +1 -1
- package/dist/cjs/utils/cli/utils/cli.helpers.js +1 -1
- package/dist/cjs/utils/cli/utils/template-generator/templates/router-template.js +9 -8
- package/dist/cjs/utils/cli/utils/template-generator/templates/router-template.js.map +1 -1
- package/dist/esm/exports/index.js.map +1 -1
- package/dist/esm/modules/auth/auth.controller.js +22 -15
- package/dist/esm/modules/auth/auth.controller.js.map +1 -1
- package/dist/esm/modules/base/base.controller.js +3 -0
- package/dist/esm/modules/base/base.controller.js.map +1 -1
- package/dist/esm/modules/base/base.middlewares.js +73 -15
- package/dist/esm/modules/base/base.middlewares.js.map +1 -1
- package/dist/esm/modules/email/email.service.js +24 -27
- package/dist/esm/modules/email/email.service.js.map +1 -1
- package/dist/esm/modules/file-upload/file-upload.controller.js +4 -0
- package/dist/esm/modules/file-upload/file-upload.controller.js.map +1 -1
- package/dist/esm/utils/arkos-router/index.js +11 -6
- package/dist/esm/utils/arkos-router/index.js.map +1 -1
- package/dist/esm/utils/arkos-router/types/index.js.map +1 -1
- package/dist/esm/utils/arkos-router/utils/helpers/index.js +1 -2
- package/dist/esm/utils/arkos-router/utils/helpers/index.js.map +1 -1
- package/dist/esm/utils/cli/utils/cli.helpers.js +1 -1
- package/dist/esm/utils/cli/utils/template-generator/templates/router-template.js +9 -8
- package/dist/esm/utils/cli/utils/template-generator/templates/router-template.js.map +1 -1
- package/dist/types/exports/index.d.ts +2 -1
- package/dist/types/modules/base/base.middlewares.d.ts +1 -2
- package/dist/types/utils/arkos-router/types/index.d.ts +2 -2
- package/package.json +1 -1
|
@@ -21,42 +21,40 @@ class EmailService {
|
|
|
21
21
|
return this.customConfig;
|
|
22
22
|
}
|
|
23
23
|
const { email: emailConfigs } = (0, server_1.getArkosConfig)();
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
const host = emailConfigs?.host || process.env.EMAIL_HOST;
|
|
25
|
+
const port = emailConfigs?.port ||
|
|
26
|
+
(process.env.EMAIL_PORT ? parseInt(process.env.EMAIL_PORT) : undefined);
|
|
27
|
+
const secure = emailConfigs?.secure !== undefined
|
|
28
|
+
? emailConfigs.secure
|
|
29
|
+
: process.env.EMAIL_SECURE
|
|
30
|
+
? process.env.EMAIL_SECURE === "true"
|
|
31
|
+
: undefined;
|
|
32
|
+
const user = emailConfigs?.auth?.user || process.env.EMAIL_USER;
|
|
33
|
+
const pass = emailConfigs?.auth?.pass || process.env.EMAIL_PASSWORD;
|
|
34
|
+
const name = emailConfigs?.name || process.env.EMAIL_NAME;
|
|
35
|
+
if (!host || !user || !pass) {
|
|
36
|
+
throw new app_error_1.default("You are trying to use emailService without setting email configurations. " +
|
|
37
|
+
"Please configure either arkosConfig.email or environment variables (EMAIL_HOST, EMAIL_USER, EMAIL_PASSWORD)", 500, {
|
|
26
38
|
docs: "Read more about emailService at https://www.arkosjs.com/docs/core-concepts/sending-emails",
|
|
27
39
|
});
|
|
28
40
|
}
|
|
29
41
|
return {
|
|
30
|
-
host
|
|
31
|
-
port:
|
|
32
|
-
secure:
|
|
42
|
+
host,
|
|
43
|
+
port: port || 465,
|
|
44
|
+
secure: secure !== undefined ? secure : true,
|
|
33
45
|
auth: {
|
|
34
|
-
user
|
|
35
|
-
pass
|
|
46
|
+
user,
|
|
47
|
+
pass,
|
|
36
48
|
},
|
|
37
|
-
name
|
|
49
|
+
name,
|
|
38
50
|
};
|
|
39
51
|
}
|
|
40
52
|
getTransporter(customConfig) {
|
|
41
|
-
if (customConfig)
|
|
42
|
-
|
|
43
|
-
return nodemailer_1.default.createTransport({
|
|
44
|
-
host: customConfig.host || defaultConfig.host,
|
|
45
|
-
port: customConfig.port || defaultConfig.port,
|
|
46
|
-
secure: customConfig.secure !== undefined
|
|
47
|
-
? customConfig.secure
|
|
48
|
-
: defaultConfig.secure,
|
|
49
|
-
auth: customConfig.auth || defaultConfig.auth,
|
|
50
|
-
});
|
|
51
|
-
}
|
|
53
|
+
if (customConfig)
|
|
54
|
+
return nodemailer_1.default.createTransport(customConfig);
|
|
52
55
|
if (!this.transporter) {
|
|
53
56
|
const config = this.getEmailConfig();
|
|
54
|
-
this.transporter = nodemailer_1.default.createTransport(
|
|
55
|
-
host: config.host,
|
|
56
|
-
port: config.port,
|
|
57
|
-
secure: config.secure,
|
|
58
|
-
auth: config.auth,
|
|
59
|
-
});
|
|
57
|
+
this.transporter = nodemailer_1.default.createTransport(config);
|
|
60
58
|
}
|
|
61
59
|
return this.transporter;
|
|
62
60
|
}
|
|
@@ -68,9 +66,8 @@ class EmailService {
|
|
|
68
66
|
const fromAddress = options.from || connectionOptions?.auth?.user || config.auth?.user;
|
|
69
67
|
if (connectionOptions || !skipVerification) {
|
|
70
68
|
const isConnected = await this.verifyConnection(transporter);
|
|
71
|
-
if (!isConnected)
|
|
69
|
+
if (!isConnected)
|
|
72
70
|
throw new Error("Failed to connect to email server");
|
|
73
|
-
}
|
|
74
71
|
}
|
|
75
72
|
const info = await transporter.sendMail({
|
|
76
73
|
...options,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"email.service.js","sourceRoot":"","sources":["../../../../src/modules/email/email.service.ts"],"names":[],"mappings":";;;;;;AAAA,4DAAqD;AACrD,+CAAuC;AACvC,yCAA8C;AAC9C,iFAAwD;AAqCxD,MAAa,YAAY;IAUvB,YAAY,MAA8B;QATlC,gBAAW,GAAuB,IAAI,CAAC;QACvC,iBAAY,GAAiC,IAAI,CAAC;QASxD,IAAI,MAAM,EAAE,CAAC;YACX,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC;QAC7B,CAAC;IACH,CAAC;IAOO,cAAc;QAEpB,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YACtB,OAAO,IAAI,CAAC,YAAY,CAAC;QAC3B,CAAC;QAGD,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,GAAG,IAAA,uBAAc,GAAE,CAAC;QAEjD,IAAI,CAAC,YAAY,EAAE,CAAC;YAClB,MAAM,IAAI,mBAAQ,CAChB,qFAAqF,EACrF,GAAG,EACH;gBACE,IAAI,EAAE,2FAA2F;aAClG,CACF,CAAC;QACJ,CAAC;QAED,OAAO;YACL,IAAI,EAAE,YAAY,CAAC,IAAI;YACvB,IAAI,EAAE,YAAY,CAAC,IAAI,IAAI,GAAG;YAC9B,MAAM,EAAE,YAAY,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI;YACtE,IAAI,EAAE;gBACJ,IAAI,EAAE,YAAY,CAAC,IAAI,EAAE,IAAI;gBAC7B,IAAI,EAAE,YAAY,CAAC,IAAI,EAAE,IAAI;aAC9B;YACD,IAAI,EAAE,YAAY,CAAC,IAAI;SACxB,CAAC;IACJ,CAAC;IAOO,cAAc,CAAC,YAAoC;QACzD,IAAI,YAAY,EAAE,CAAC;YAEjB,MAAM,aAAa,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC;YAC5C,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AM,IAAI,CAAC,gBAAgB,CAAC,WAAW,CAAC,CAAC;YAE7D,IAAI,CAAC,WAAW,EAAE,CAAC;gBACjB,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;YACvD,CAAC;QACH,CAAC;QAGD,MAAM,IAAI,GAAG,MAAM,WAAW,CAAC,QAAQ,CAAC;YACtC,GAAG,OAAO;YACV,IAAI,EAAE,WAAW;YACjB,IAAI,EAAE,OAAO,EAAE,IAAI,IAAI,IAAA,sBAAO,EAAC,OAAO,CAAC,IAAI,CAAC;SAC7C,CAAC,CAAC;QAEH,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,CAAC,SAAS,EAAE,CAAC;IACtD,CAAC;IAOM,KAAK,CAAC,gBAAgB,CAC3B,mBAAiC;QAEjC,IAAI,CAAC;YACH,MAAM,WAAW,GAAG,mBAAmB,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;YACjE,MAAM,WAAW,CAAC,MAAM,EAAE,CAAC;YAC3B,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,gCAAgC,EAAE,KAAK,CAAC,CAAC;YACvD,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IAMM,YAAY,CAAC,MAA6B;QAC/C,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC;QAC3B,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;IAC1B,CAAC;IAOM,MAAM,CAAC,MAAM,CAAC,MAA6B;QAChD,OAAO,IAAI,YAAY,CAAC,MAAM,CAAC,CAAC;IAClC,CAAC;CACF;AAnKD,oCAmKC;AAGD,MAAM,YAAY,GAAG,IAAI,YAAY,EAAE,CAAC;AACxC,kBAAe,YAAY,CAAC","sourcesContent":["import nodemailer, { Transporter } from \"nodemailer\";\nimport { convert } from \"html-to-text\";\nimport { getArkosConfig } from \"../../server\";\nimport AppError from \"../error-handler/utils/app-error\";\n\n/**\n * Defines the options for sending an email.\n */\nexport type EmailOptions = {\n from?: string; // Sender's email address (optional).\n to: string | string[]; // Recipient's email address or an array of email addresses.\n subject: string; // Subject of the email.\n text?: string; // Plain text body of the email (optional).\n html: string; // HTML body of the email.\n};\n\n/**\n * Defines the authentication options for SMTP.\n */\nexport type SMTPAuthOptions = {\n user: string;\n pass: string;\n};\n\n/**\n * Defines the connection options for SMTP server.\n */\nexport type SMTPConnectionOptions = {\n host?: string;\n port?: number;\n secure?: boolean;\n auth?: SMTPAuthOptions;\n name?: string;\n};\n\n/**\n * A service class to handle email-related tasks, including sending emails.\n *\n * See the api reference [www.arkosjs.com/docs/api-reference/the-email-service-class](https://www.arkosjs.com/docs/api-reference/the-email-service-class)\n */\nexport class EmailService {\n private transporter: Transporter | null = null;\n private customConfig: SMTPConnectionOptions | null = null;\n\n /**\n * Creates an instance of the EmailService class.\n *\n * @param {SMTPConnectionOptions} [config] - Optional custom SMTP configuration.\n * If provided, these settings will be used instead of the Arkos config.\n */\n constructor(config?: SMTPConnectionOptions) {\n if (config) {\n this.customConfig = config;\n }\n }\n\n /**\n * Gets the email configuration, either from constructor-provided config or ArkosConfig\n * @returns Configuration object with host, port, and auth details\n * @throws AppError if email configuration is not set\n */\n private getEmailConfig(): SMTPConnectionOptions {\n // If custom config was provided through constructor, use it\n if (this.customConfig) {\n return this.customConfig;\n }\n\n // Otherwise, get from Arkos config\n const { email: emailConfigs } = getArkosConfig();\n\n if (!emailConfigs) {\n throw new AppError(\n \"You are trying to use emailService without setting arkosConfig.email configurations\",\n 500,\n {\n docs: \"Read more about emailService at https://www.arkosjs.com/docs/core-concepts/sending-emails\",\n }\n );\n }\n\n return {\n host: emailConfigs.host,\n port: emailConfigs.port || 465,\n secure: emailConfigs.secure !== undefined ? emailConfigs.secure : true,\n auth: {\n user: emailConfigs.auth?.user,\n pass: emailConfigs.auth?.pass,\n },\n name: emailConfigs.name,\n };\n }\n\n /**\n * Gets or creates a transporter using provided config or default config\n * @param customConfig Optional override connection settings\n * @returns A configured nodemailer transporter\n */\n private getTransporter(customConfig?: SMTPConnectionOptions): Transporter {\n if (customConfig) {\n // Create temporary transporter with custom settings\n const defaultConfig = this.getEmailConfig();\n return nodemailer.createTransport({\n host: customConfig.host || defaultConfig.host,\n port: customConfig.port || defaultConfig.port,\n secure:\n customConfig.secure !== undefined\n ? customConfig.secure\n : defaultConfig.secure,\n auth: customConfig.auth || defaultConfig.auth,\n });\n }\n\n // Use cached transporter or create new one with default settings\n if (!this.transporter) {\n const config = this.getEmailConfig();\n this.transporter = nodemailer.createTransport({\n host: config.host,\n port: config.port,\n secure: config.secure,\n auth: config.auth,\n });\n }\n\n return this.transporter;\n }\n\n /**\n * Sends an email with the provided options.\n * Can use either the default configuration or custom connection options.\n *\n * @param {EmailOptions} options - The options for the email to be sent.\n * @param {SMTPConnectionOptions} [connectionOptions] - Optional custom connection settings.\n * @param {boolean} [skipVerification=false] - Whether to skip connection verification.\n * @returns {Promise<{ success: boolean; messageId?: string }>} Result with message ID on success.\n */\n public async send(\n options: EmailOptions,\n connectionOptions?: SMTPConnectionOptions,\n skipVerification = false\n ): Promise<{ success: boolean; messageId?: string }> {\n const config = this.getEmailConfig();\n const transporter = connectionOptions\n ? this.getTransporter(connectionOptions)\n : this.getTransporter();\n\n // Determine from address with proper fallbacks\n const fromAddress =\n options.from || connectionOptions?.auth?.user || config.auth?.user;\n\n // Optionally verify connection\n if (connectionOptions || !skipVerification) {\n const isConnected = await this.verifyConnection(transporter);\n\n if (!isConnected) {\n throw new Error(\"Failed to connect to email server\");\n }\n }\n\n // Send the email\n const info = await transporter.sendMail({\n ...options,\n from: fromAddress,\n text: options?.text || convert(options.html),\n });\n\n return { success: true, messageId: info.messageId };\n }\n\n /**\n * Verifies the connection to the email server.\n * @param {Transporter} [transporterToVerify] - Optional transporter to verify.\n * @returns {Promise<boolean>} A promise that resolves to true if connection is valid.\n */\n public async verifyConnection(\n transporterToVerify?: Transporter\n ): Promise<boolean> {\n try {\n const transporter = transporterToVerify || this.getTransporter();\n await transporter.verify();\n return true;\n } catch (error) {\n console.error(\"Email Server Connection Failed\", error);\n return false;\n }\n }\n\n /**\n * Updates the custom configuration for this email service instance.\n * @param {SMTPConnectionOptions} config - The new connection options.\n */\n public updateConfig(config: SMTPConnectionOptions): void {\n this.customConfig = config;\n this.transporter = null; // Reset transporter so it will be recreated with new config\n }\n\n /**\n * Creates a new instance of EmailService with custom configuration.\n * @param {SMTPConnectionOptions} config - The connection options for the new instance.\n * @returns {EmailService} A new EmailService instance.\n */\n public static create(config: SMTPConnectionOptions): EmailService {\n return new EmailService(config);\n }\n}\n\n// Create default instance\nconst emailService = new EmailService();\nexport default emailService;\n"]}
|
|
1
|
+
{"version":3,"file":"email.service.js","sourceRoot":"","sources":["../../../../src/modules/email/email.service.ts"],"names":[],"mappings":";;;;;;AAAA,4DAAqD;AACrD,+CAAuC;AACvC,yCAA8C;AAC9C,iFAAwD;AAqCxD,MAAa,YAAY;IAUvB,YAAY,MAA8B;QATlC,gBAAW,GAAuB,IAAI,CAAC;QACvC,iBAAY,GAAiC,IAAI,CAAC;QASxD,IAAI,MAAM,EAAE,CAAC;YACX,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC;QAC7B,CAAC;IACH,CAAC;IAUO,cAAc;QACpB,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YACtB,OAAO,IAAI,CAAC,YAAY,CAAC;QAC3B,CAAC;QAED,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,GAAG,IAAA,uBAAc,GAAE,CAAC;QACjD,MAAM,IAAI,GAAG,YAAY,EAAE,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC;QAC1D,MAAM,IAAI,GACR,YAAY,EAAE,IAAI;YAClB,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAA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AK,CAAC,CAAC;YACvD,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IAMM,YAAY,CAAC,MAA6B;QAC/C,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC;QAC3B,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;IAC1B,CAAC;IAOM,MAAM,CAAC,MAAM,CAAC,MAA6B;QAChD,OAAO,IAAI,YAAY,CAAC,MAAM,CAAC,CAAC;IAClC,CAAC;CACF;AA3JD,oCA2JC;AAED,MAAM,YAAY,GAAG,IAAI,YAAY,EAAE,CAAC;AAExC,kBAAe,YAAY,CAAC","sourcesContent":["import nodemailer, { Transporter } from \"nodemailer\";\nimport { convert } from \"html-to-text\";\nimport { getArkosConfig } from \"../../server\";\nimport AppError from \"../error-handler/utils/app-error\";\n\n/**\n * Defines the options for sending an email.\n */\nexport type EmailOptions = {\n from?: string;\n to: string | string[];\n subject: string;\n text?: string;\n html: string;\n};\n\n/**\n * Defines the authentication options for SMTP.\n */\nexport type SMTPAuthOptions = {\n user: string;\n pass: string;\n};\n\n/**\n * Defines the connection options for SMTP server.\n */\nexport type SMTPConnectionOptions = {\n host?: string;\n port?: number;\n secure?: boolean;\n auth?: SMTPAuthOptions;\n name?: string;\n};\n\n/**\n * A service class to handle email-related tasks, including sending emails.\n *\n * See the api reference [www.arkosjs.com/docs/api-reference/the-email-service-class](https://www.arkosjs.com/docs/api-reference/the-email-service-class)\n */\nexport class EmailService {\n private transporter: Transporter | null = null;\n private customConfig: SMTPConnectionOptions | null = null;\n\n /**\n * Creates an instance of the EmailService class.\n *\n * @param {SMTPConnectionOptions} [config] - Optional custom SMTP configuration.\n * If provided, these settings will be used instead of the Arkos config.\n */\n constructor(config?: SMTPConnectionOptions) {\n if (config) {\n this.customConfig = config;\n }\n }\n\n /**\n * Gets the email configuration from multiple sources with priority:\n * 1. Constructor customConfig\n * 2. ArkosConfig\n * 3. Environment variables\n * @returns Configuration object with host, port, and auth details\n * @throws AppError if required email configuration is not set\n */\n private getEmailConfig(): SMTPConnectionOptions {\n if (this.customConfig) {\n return this.customConfig;\n }\n\n const { email: emailConfigs } = getArkosConfig();\n const host = emailConfigs?.host || process.env.EMAIL_HOST;\n const port =\n emailConfigs?.port ||\n (process.env.EMAIL_PORT ? parseInt(process.env.EMAIL_PORT) : undefined);\n const secure =\n emailConfigs?.secure !== undefined\n ? emailConfigs.secure\n : process.env.EMAIL_SECURE\n ? process.env.EMAIL_SECURE === \"true\"\n : undefined;\n const user = emailConfigs?.auth?.user || process.env.EMAIL_USER;\n const pass = emailConfigs?.auth?.pass || process.env.EMAIL_PASSWORD;\n const name = emailConfigs?.name || process.env.EMAIL_NAME;\n\n if (!host || !user || !pass) {\n throw new AppError(\n \"You are trying to use emailService without setting email configurations. \" +\n \"Please configure either arkosConfig.email or environment variables (EMAIL_HOST, EMAIL_USER, EMAIL_PASSWORD)\",\n 500,\n {\n docs: \"Read more about emailService at https://www.arkosjs.com/docs/core-concepts/sending-emails\",\n }\n );\n }\n\n return {\n host,\n port: port || 465,\n secure: secure !== undefined ? secure : true,\n auth: {\n user,\n pass,\n },\n name,\n };\n }\n\n /**\n * Gets or creates a transporter using the email configuration\n * @param customConfig Optional override connection settings (takes full priority if provided)\n * @returns A configured nodemailer transporter\n */\n private getTransporter(customConfig?: SMTPConnectionOptions): Transporter {\n if (customConfig) return nodemailer.createTransport(customConfig);\n\n if (!this.transporter) {\n const config = this.getEmailConfig();\n this.transporter = nodemailer.createTransport(config);\n }\n return this.transporter;\n }\n\n /**\n * Sends an email with the provided options.\n * Can use either the default configuration or custom connection options.\n *\n * @param {EmailOptions} options - The options for the email to be sent.\n * @param {SMTPConnectionOptions} [connectionOptions] - Optional custom connection settings.\n * @param {boolean} [skipVerification=false] - Whether to skip connection verification.\n * @returns {Promise<{ success: boolean; messageId?: string }>} Result with message ID on success.\n */\n public async send(\n options: EmailOptions,\n connectionOptions?: SMTPConnectionOptions,\n skipVerification = false\n ): Promise<{ success: boolean; messageId?: string }> {\n const config = this.getEmailConfig();\n const transporter = connectionOptions\n ? this.getTransporter(connectionOptions)\n : this.getTransporter();\n\n const fromAddress =\n options.from || connectionOptions?.auth?.user || config.auth?.user;\n\n if (connectionOptions || !skipVerification) {\n const isConnected = await this.verifyConnection(transporter);\n\n if (!isConnected) throw new Error(\"Failed to connect to email server\");\n }\n\n // Send the email\n const info = await transporter.sendMail({\n ...options,\n from: fromAddress,\n text: options?.text || convert(options.html),\n });\n\n return { success: true, messageId: info.messageId };\n }\n\n /**\n * Verifies the connection to the email server.\n * @param {Transporter} [transporterToVerify] - Optional transporter to verify.\n * @returns {Promise<boolean>} A promise that resolves to true if connection is valid.\n */\n public async verifyConnection(\n transporterToVerify?: Transporter\n ): Promise<boolean> {\n try {\n const transporter = transporterToVerify || this.getTransporter();\n await transporter.verify();\n return true;\n } catch (error) {\n console.error(\"Email Server Connection Failed\", error);\n return false;\n }\n }\n\n /**\n * Updates the custom configuration for this email service instance.\n * @param {SMTPConnectionOptions} config - The new connection options.\n */\n public updateConfig(config: SMTPConnectionOptions): void {\n this.customConfig = config;\n this.transporter = null; // Reset transporter so it will be recreated with new config\n }\n\n /**\n * Creates a new instance of EmailService with custom configuration.\n * @param {SMTPConnectionOptions} config - The connection options for the new instance.\n * @returns {EmailService} A new EmailService instance.\n */\n public static create(config: SMTPConnectionOptions): EmailService {\n return new EmailService(config);\n }\n}\n\nconst emailService = new EmailService();\n\nexport default emailService;\n"]}
|
|
@@ -79,8 +79,10 @@ class FileUploadController {
|
|
|
79
79
|
: "File uploaded successfully",
|
|
80
80
|
};
|
|
81
81
|
if (this.interceptors?.afterUploadFile) {
|
|
82
|
+
res.originalData = jsonContent;
|
|
82
83
|
req.responseData = jsonContent;
|
|
83
84
|
res.locals.data = jsonContent;
|
|
85
|
+
res.originalStatus = 200;
|
|
84
86
|
req.responseStatus = 200;
|
|
85
87
|
res.locals.status = 200;
|
|
86
88
|
return next();
|
|
@@ -221,8 +223,10 @@ class FileUploadController {
|
|
|
221
223
|
: "File uploaded successfully",
|
|
222
224
|
};
|
|
223
225
|
if (this.interceptors.afterUpdateFile) {
|
|
226
|
+
res.originalData = jsonContent;
|
|
224
227
|
req.responseData = jsonContent;
|
|
225
228
|
res.locals.data = jsonContent;
|
|
229
|
+
res.originalStatus = 200;
|
|
226
230
|
req.responseStatus = 200;
|
|
227
231
|
res.locals.status = 200;
|
|
228
232
|
return next();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-upload.controller.js","sourceRoot":"","sources":["../../../../src/modules/file-upload/file-upload.controller.ts"],"names":[],"mappings":";;;;;;AAAA,iFAAwD;AACxD,+DAG+B;AAC/B,gDAAwB;AACxB,4CAAoB;AACpB,qFAA4D;AAC5D,yCAA8C;AAC9C,6EAAgF;AAEhF,+DAAiE;AAKjE,MAAa,oBAAoB;IAAjC;QAeE,eAAU,GAAG,IAAA,qBAAU,EACrB,KAAK,EAAE,GAAiB,EAAE,GAAkB,EAAE,IAAuB,EAAE,EAAE;YACvE,IAAI,CAAC,YAAY;gBACf,IAAA,oCAAmB,EAAC,aAAa,CAAC,EAAE,YAAY,IAAI,EAAE,CAAC;YAEzD,MAAM,EAAE,QAAQ,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC;YAChC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC;YACtD,MAAM,OAAO,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC;YAEpD,MAAM,EACJ,qBAAqB,EACrB,iBAAiB,EACjB,kBAAkB,EAClB,kBAAkB,GACnB,GAAG,IAAA,2CAAqB,GAAE,CAAC;YAE5B,MAAM,EAAE,UAAU,EAAE,GAAG,IAAA,uBAAc,GAAE,CAAC;YACxC,MAAM,aAAa,GAAG,UAAU,EAAE,aAAa,IAAI,UAAU,CAAC;YAE9D,MAAM,UAAU,GAAG,cAAI,CAAC,OAAO,CAAC,O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,OAAO,CAAC,IAAI,CAAC;wBAC1B,CAAC,CAAC,QAAQ,IAAI,QAAQ,CAAC,IAAI,EAAE,KAAK,EAAE;4BAClC,CAAC,CAAC,8BAA8B,IAAI,CAAC,MAAM,qBAAqB;4BAChE,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,8BAA8B;wBAChD,CAAC,CAAC,QAAQ,IAAI,QAAQ,CAAC,IAAI,EAAE,KAAK,EAAE;4BAClC,CAAC,CAAC,2BAA2B;4BAC7B,CAAC,CAAC,4BAA4B;iBACnC,CAAC;gBAEF,IAAI,IAAI,CAAC,YAAY,CAAC,eAAe,EAAE,CAAC;oBACtC,GAAG,CAAC,YAAY,GAAG,WAAW,CAAC;oBAC/B,GAAG,CAAC,MAAM,CAAC,IAAI,GAAG,WAAW,CAAC;oBAC9B,GAAG,CAAC,cAAc,GAAG,GAAG,CAAC;oBACzB,GAAG,CAAC,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC;oBACxB,OAAO,IAAI,EAAE,CAAC;gBAChB,CAAC;gBAED,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;YACpC,CAAC,CAAC,CAAC;QACL,CAAC,CACF,CAAC;IAqDJ,CAAC;CAAA;AA7XD,oDA6XC;AAcD,MAAM,oBAAoB,GAAG,IAAI,oBAAoB,EAAE,CAAC;AAExD,kBAAe,oBAAoB,CAAC","sourcesContent":["import AppError from \"../error-handler/utils/app-error\";\nimport {\n FileUploadService,\n getFileUploadServices,\n} from \"./file-upload.service\";\nimport path from \"path\";\nimport fs from \"fs\";\nimport catchAsync from \"../error-handler/utils/catch-async\";\nimport { getArkosConfig } from \"../../server\";\nimport { processFile, processImage } from \"./utils/helpers/file-upload.helpers\";\nimport { ArkosNextFunction, ArkosRequest, ArkosResponse } from \"../../types\";\nimport { getModuleComponents } from \"../../utils/dynamic-loader\";\n\n/**\n * Handles files uploads and allow to be extended\n */\nexport class FileUploadController {\n /**\n * Model-specific interceptors loaded from model modules\n * @private\n */\n private interceptors: any;\n\n /**\n * Handles file upload requests, processes images if needed, and returns URLs\n *\n * Supports paths outside of the project directory with '../' prefix\n * @param {ArkosRequest} req - Arkos request object\n * @param {ArkosResponse} res - Arkos response object\n * @param {ArkosNextFunction} next - Arkos next middleware function\n */\n uploadFile = catchAsync(\n async (req: ArkosRequest, res: ArkosResponse, next: ArkosNextFunction) => {\n this.interceptors =\n getModuleComponents(\"file-upload\")?.interceptors || {};\n\n const { fileType } = req.params;\n const { format, width, height, resizeTo } = req.query;\n const options = { format, width, height, resizeTo };\n\n const {\n documentUploadService,\n fileUploadService,\n imageUploadService,\n videoUploadService,\n } = getFileUploadServices();\n\n const { fileUpload } = getArkosConfig();\n const baseUploadDir = fileUpload?.baseUploadDir || \"/uploads\";\n\n const uploadPath = path.resolve(process.cwd(), baseUploadDir, fileType);\n try {\n await fs.promises.access(uploadPath);\n } catch (err) {\n await fs.promises.mkdir(uploadPath, { recursive: true });\n }\n\n let uploader: FileUploadService;\n switch (fileType) {\n case \"images\":\n uploader = imageUploadService;\n break;\n case \"videos\":\n uploader = videoUploadService;\n break;\n case \"documents\":\n uploader = documentUploadService;\n break;\n case \"files\":\n uploader = fileUploadService;\n break;\n default:\n return next(new AppError(\"Invalid file type\", 400));\n }\n\n uploader.handleMultipleUpload()(req, res, async (err) => {\n if (err) return next(err);\n\n let data;\n if (req.files && Array.isArray(req.files) && req.files.length > 0) {\n if (fileType === \"images\") {\n data = await Promise.all(\n req.files.map((file) => processImage(req, file.path, options))\n );\n } else {\n data = await Promise.all(\n req.files.map((file) => processFile(req, file.path))\n );\n }\n data = data.filter((url) => url !== null);\n } else if (req.file) {\n if (fileType === \"images\") {\n data = await processImage(req, req.file.path, options);\n } else {\n data = await processFile(req, req.file.path);\n }\n } else {\n return next(\n new AppError(\n `No file or files were attached on field ${fileType} on the request body as form data.`,\n 400,\n {},\n \"NoFileOrFilesAttached\"\n )\n );\n }\n\n const jsonContent = {\n success: true,\n data,\n message: Array.isArray(data)\n ? `${data.length} files uploaded successfully`\n : \"File uploaded successfully\",\n };\n\n if (this.interceptors?.afterUploadFile) {\n req.responseData = jsonContent;\n res.locals.data = jsonContent;\n req.responseStatus = 200;\n res.locals.status = 200;\n return next();\n }\n\n res.status(200).json(jsonContent);\n });\n }\n );\n\n /**\n * Handles file deletion requests\n *\n * Supports paths outside of the project directory with '../' prefix\n * @param {ArkosRequest} req - Arkos request object\n * @param {ArkosResponse} res - Arkos response object\n * @param {ArkosNextFunction} next - Arkos next middleware function\n */\n deleteFile = catchAsync(\n async (req: ArkosRequest, res: ArkosResponse, next: ArkosNextFunction) => {\n this.interceptors =\n getModuleComponents(\"file-upload\")?.interceptors || {};\n\n const { fileType, fileName } = req.params;\n\n const {\n documentUploadService,\n fileUploadService,\n imageUploadService,\n videoUploadService,\n } = getFileUploadServices();\n\n let uploader: FileUploadService;\n switch (fileType) {\n case \"images\":\n uploader = imageUploadService;\n break;\n case \"videos\":\n uploader = videoUploadService;\n break;\n case \"documents\":\n uploader = documentUploadService;\n break;\n case \"files\":\n uploader = fileUploadService;\n break;\n default:\n return next(new AppError(\"Invalid file type\", 400));\n }\n\n try {\n const { fileUpload } = getArkosConfig();\n const baseUploadRoute = fileUpload?.baseRoute || \"/api/uploads\";\n\n const urlPattern = new RegExp(\n `${baseUploadRoute}/${fileType}/${fileName}`\n );\n\n const isExpectedUrlPattern = urlPattern.test(req.originalUrl);\n\n if (isExpectedUrlPattern) {\n // Build the expected URL for this request\n const fullUrl = `${req.protocol}://${req.get(\"host\")}${\n req.originalUrl\n }`;\n\n await uploader.deleteFileByUrl(fullUrl);\n } else {\n await uploader.deleteFileByName(fileName, fileType);\n }\n\n if (this.interceptors.afterDeleteFile) {\n req.responseStatus = 204;\n return next();\n }\n\n res.status(204).json();\n } catch (error) {\n if (error instanceof AppError) return next(error);\n\n return next(new AppError(\"File not found\", 404));\n }\n }\n );\n\n /**\n * Handles file update requests by deleting the old file and uploading a new one\n * @param {ArkosRequest} req - Arkos request object\n * @param {ArkosResponse} res - Arkos response object\n * @param {ArkosNextFunction} next - Arkos next middleware function\n */\n updateFile = catchAsync(\n async (req: ArkosRequest, res: ArkosResponse, next: ArkosNextFunction) => {\n this.interceptors =\n getModuleComponents(\"file-upload\")?.interceptors || {};\n\n const { fileType, fileName } = req.params;\n const { format, width, height, resizeTo } = req.query;\n const options = { format, width, height, resizeTo };\n\n const {\n documentUploadService,\n fileUploadService,\n imageUploadService,\n videoUploadService,\n } = getFileUploadServices();\n\n const { fileUpload } = getArkosConfig();\n const baseUploadDir = fileUpload?.baseUploadDir || \"/uploads\";\n\n // Ensure upload directory exists\n const uploadPath = path.resolve(process.cwd(), baseUploadDir, fileType);\n try {\n await fs.promises.access(uploadPath);\n } catch (err) {\n // Create directory if it doesn't exist\n await fs.promises.mkdir(uploadPath, { recursive: true });\n }\n\n // Select the appropriate uploader service based on file type\n let uploader: FileUploadService;\n switch (fileType) {\n case \"images\":\n uploader = imageUploadService;\n break;\n case \"videos\":\n uploader = videoUploadService;\n break;\n case \"documents\":\n uploader = documentUploadService;\n break;\n case \"files\":\n uploader = fileUploadService;\n break;\n default:\n return next(new AppError(\"Invalid file type\", 400));\n }\n\n // Handle the file upload\n uploader.handleMultipleUpload()(req, res, async (err) => {\n if (err) return next(err);\n\n // Check if new file was uploaded\n if (\n !req.file &&\n (!req.files || !Array.isArray(req.files) || req.files.length === 0)\n ) {\n return next(new AppError(\"No new file uploaded\", 400));\n }\n\n // Only attempt to delete old file if fileName is specified\n if (fileName && fileName.trim() !== \"\") {\n try {\n const baseUploadRoute = fileUpload?.baseRoute || \"/api/uploads\";\n\n // Check if the URL follows the expected format\n const urlPattern = new RegExp(\n `${baseUploadRoute}/${fileType}/${fileName}`\n );\n\n const isExpectedUrlPattern = urlPattern.test(req.originalUrl);\n\n if (isExpectedUrlPattern) {\n // URL matches expected pattern, use deleteFileByUrl\n const oldFileUrl = `${req.protocol}://${req.get(\"host\")}${\n req.originalUrl\n }`;\n await uploader.deleteFileByUrl(oldFileUrl);\n } else {\n // URL doesn't match expected pattern, use deleteFileByName\n await uploader.deleteFileByName(fileName, fileType);\n }\n } catch (error) {\n // Log the error but continue with upload - old file might not exist\n console.warn(`Could not delete old file: ${fileName}`, error);\n }\n }\n\n // Process the new uploaded file(s)\n let data;\n if (req.files && Array.isArray(req.files) && req.files.length > 0) {\n if (fileType === \"images\") {\n // Process multiple image files with image transformations\n data = await Promise.all(\n req.files.map((file) => processImage(req, file.path, options))\n );\n } else {\n // Just store other file types without processing\n data = await Promise.all(\n req.files.map((file) => processFile(req, file.path))\n );\n }\n // Filter out any null values from failed processing\n data = data.filter((url) => url !== null);\n } else if (req.file) {\n // Process a single file\n if (fileType === \"images\") {\n data = await processImage(req, req.file.path, options);\n } else {\n data = await processFile(req, req.file.path);\n }\n }\n\n const jsonContent = {\n success: true,\n data,\n message: Array.isArray(data)\n ? fileName && fileName.trim() !== \"\"\n ? `File updated successfully. ${data.length} new files uploaded`\n : `${data.length} files uploaded successfully`\n : fileName && fileName.trim() !== \"\"\n ? \"File updated successfully\"\n : \"File uploaded successfully\",\n };\n\n if (this.interceptors.afterUpdateFile) {\n req.responseData = jsonContent;\n res.locals.data = jsonContent;\n req.responseStatus = 200;\n res.locals.status = 200;\n return next();\n }\n\n res.status(200).json(jsonContent);\n });\n }\n );\n\n /**\n * Not implemented yet\n *\n * @deprecated\n */\n // streamFile = catchAsync(\n // async (req: ArkosRequest, res: ArkosResponse, _: ArkosNextFunction) => {\n // const { fileName, fileType } = req.params;\n\n // const filePath = path.join(\".\", \"uploads\", fileType, fileName);\n // try {\n // await fs.promises.access(filePath);\n // } catch (err) {\n // throw new AppError(\"File not found\", 404);\n // }\n\n // const fileStat = await fs.promises.stat(filePath);\n // const fileSize = fileStat.size;\n // const range = req.headers.range;\n\n // if (range) {\n // const [partialStart, partialEnd] = range\n // .replace(/bytes=/, \"\")\n // .split(\"-\");\n // const start = parseInt(partialStart, 10) || 0;\n // const end = partialEnd ? parseInt(partialEnd, 10) : fileSize - 1;\n\n // if (start >= fileSize || end >= fileSize) {\n // res.status(416).json({ error: \"Range Not Satisfiable\" });\n // return;\n // }\n\n // res.writeHead(206, {\n // \"Content-Range\": `bytes ${start}-${end}/${fileSize}`,\n // \"Accept-Ranges\": \"bytes\",\n // \"Content-Length\": end - start + 1,\n // \"Content-Type\": \"application/octet-stream\",\n // \"Content-Disposition\": `inline; filename=\"${fileName}\"`,\n // });\n\n // fs.createReadStream(filePath, { start, end }).pipe(res);\n // } else {\n // res.writeHead(200, {\n // \"Content-Length\": fileSize,\n // \"Content-Type\": \"application/octet-stream\",\n // \"Content-Disposition\": `inline; filename=\"${fileName}\"`,\n // });\n // fs.createReadStream(filePath).pipe(res);\n // }\n // }\n // );\n}\n\n/**\n * Controller instance responsible for handling file upload operations.\n * Manages the processing and routing of file upload requests.\n *\n * @remarks\n * This controller handles various file upload operations including validation,\n * storage, and response management.\n *\n * @instance\n * @constant\n * @see {@link https://www.arkosjs.com/docs/api-reference/file-upload-controller-object}\n */\nconst fileUploadController = new FileUploadController();\n\nexport default fileUploadController;\n"]}
|
|
1
|
+
{"version":3,"file":"file-upload.controller.js","sourceRoot":"","sources":["../../../../src/modules/file-upload/file-upload.controller.ts"],"names":[],"mappings":";;;;;;AAAA,iFAAwD;AACxD,+DAG+B;AAC/B,gDAAwB;AACxB,4CAAoB;AACpB,qFAA4D;AAC5D,yCAA8C;AAC9C,6EAAgF;AAEhF,+DAAiE;AAKjE,MAAa,oBAAoB;IAAjC;QAeE,eAAU,GAAG,IAAA,qBAAU,EACrB,KAAK,EAAE,GAAiB,EAAE,GAAkB,EAAE,IAAuB,EAAE,EAAE;YACvE,IAAI,CAAC,YAAY;gBACf,IAAA,oCAAmB,EAAC,aAAa,CAAC,EAAE,YAAY,IAAI,EAAE,CAAC;YAEzD,MAAM,EAAE,QAAQ,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC;YAChC,MAAM,EAAE,MAAM,EAAE,KAAK,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BAChB,CAAC;gBAED,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;YACpC,CAAC,CAAC,CAAC;QACL,CAAC,CACF,CAAC;IAqDJ,CAAC;CAAA;AAjYD,oDAiYC;AAcD,MAAM,oBAAoB,GAAG,IAAI,oBAAoB,EAAE,CAAC;AAExD,kBAAe,oBAAoB,CAAC","sourcesContent":["import AppError from \"../error-handler/utils/app-error\";\nimport {\n FileUploadService,\n getFileUploadServices,\n} from \"./file-upload.service\";\nimport path from \"path\";\nimport fs from \"fs\";\nimport catchAsync from \"../error-handler/utils/catch-async\";\nimport { getArkosConfig } from \"../../server\";\nimport { processFile, processImage } from \"./utils/helpers/file-upload.helpers\";\nimport { ArkosNextFunction, ArkosRequest, ArkosResponse } from \"../../types\";\nimport { getModuleComponents } from \"../../utils/dynamic-loader\";\n\n/**\n * Handles files uploads and allow to be extended\n */\nexport class FileUploadController {\n /**\n * Model-specific interceptors loaded from model modules\n * @private\n */\n private interceptors: any;\n\n /**\n * Handles file upload requests, processes images if needed, and returns URLs\n *\n * Supports paths outside of the project directory with '../' prefix\n * @param {ArkosRequest} req - Arkos request object\n * @param {ArkosResponse} res - Arkos response object\n * @param {ArkosNextFunction} next - Arkos next middleware function\n */\n uploadFile = catchAsync(\n async (req: ArkosRequest, res: ArkosResponse, next: ArkosNextFunction) => {\n this.interceptors =\n getModuleComponents(\"file-upload\")?.interceptors || {};\n\n const { fileType } = req.params;\n const { format, width, height, resizeTo } = req.query;\n const options = { format, width, height, resizeTo };\n\n const {\n documentUploadService,\n fileUploadService,\n imageUploadService,\n videoUploadService,\n } = getFileUploadServices();\n\n const { fileUpload } = getArkosConfig();\n const baseUploadDir = fileUpload?.baseUploadDir || \"/uploads\";\n\n const uploadPath = path.resolve(process.cwd(), baseUploadDir, fileType);\n try {\n await fs.promises.access(uploadPath);\n } catch (err) {\n await fs.promises.mkdir(uploadPath, { recursive: true });\n }\n\n let uploader: FileUploadService;\n switch (fileType) {\n case \"images\":\n uploader = imageUploadService;\n break;\n case \"videos\":\n uploader = videoUploadService;\n break;\n case \"documents\":\n uploader = documentUploadService;\n break;\n case \"files\":\n uploader = fileUploadService;\n break;\n default:\n return next(new AppError(\"Invalid file type\", 400));\n }\n\n uploader.handleMultipleUpload()(req, res, async (err) => {\n if (err) return next(err);\n\n let data;\n if (req.files && Array.isArray(req.files) && req.files.length > 0) {\n if (fileType === \"images\") {\n data = await Promise.all(\n req.files.map((file) => processImage(req, file.path, options))\n );\n } else {\n data = await Promise.all(\n req.files.map((file) => processFile(req, file.path))\n );\n }\n data = data.filter((url) => url !== null);\n } else if (req.file) {\n if (fileType === \"images\") {\n data = await processImage(req, req.file.path, options);\n } else {\n data = await processFile(req, req.file.path);\n }\n } else {\n return next(\n new AppError(\n `No file or files were attached on field ${fileType} on the request body as form data.`,\n 400,\n {},\n \"NoFileOrFilesAttached\"\n )\n );\n }\n\n const jsonContent = {\n success: true,\n data,\n message: Array.isArray(data)\n ? `${data.length} files uploaded successfully`\n : \"File uploaded successfully\",\n };\n\n if (this.interceptors?.afterUploadFile) {\n (res as any).originalData = jsonContent;\n req.responseData = jsonContent;\n res.locals.data = jsonContent;\n (res as any).originalStatus = 200;\n req.responseStatus = 200;\n res.locals.status = 200;\n return next();\n }\n\n res.status(200).json(jsonContent);\n });\n }\n );\n\n /**\n * Handles file deletion requests\n *\n * Supports paths outside of the project directory with '../' prefix\n * @param {ArkosRequest} req - Arkos request object\n * @param {ArkosResponse} res - Arkos response object\n * @param {ArkosNextFunction} next - Arkos next middleware function\n */\n deleteFile = catchAsync(\n async (req: ArkosRequest, res: ArkosResponse, next: ArkosNextFunction) => {\n this.interceptors =\n getModuleComponents(\"file-upload\")?.interceptors || {};\n\n const { fileType, fileName } = req.params;\n\n const {\n documentUploadService,\n fileUploadService,\n imageUploadService,\n videoUploadService,\n } = getFileUploadServices();\n\n let uploader: FileUploadService;\n switch (fileType) {\n case \"images\":\n uploader = imageUploadService;\n break;\n case \"videos\":\n uploader = videoUploadService;\n break;\n case \"documents\":\n uploader = documentUploadService;\n break;\n case \"files\":\n uploader = fileUploadService;\n break;\n default:\n return next(new AppError(\"Invalid file type\", 400));\n }\n\n try {\n const { fileUpload } = getArkosConfig();\n const baseUploadRoute = fileUpload?.baseRoute || \"/api/uploads\";\n\n const urlPattern = new RegExp(\n `${baseUploadRoute}/${fileType}/${fileName}`\n );\n\n const isExpectedUrlPattern = urlPattern.test(req.originalUrl);\n\n if (isExpectedUrlPattern) {\n // Build the expected URL for this request\n const fullUrl = `${req.protocol}://${req.get(\"host\")}${\n req.originalUrl\n }`;\n\n await uploader.deleteFileByUrl(fullUrl);\n } else {\n await uploader.deleteFileByName(fileName, fileType);\n }\n\n if (this.interceptors.afterDeleteFile) {\n req.responseStatus = 204;\n return next();\n }\n\n res.status(204).json();\n } catch (error) {\n if (error instanceof AppError) return next(error);\n\n return next(new AppError(\"File not found\", 404));\n }\n }\n );\n\n /**\n * Handles file update requests by deleting the old file and uploading a new one\n * @param {ArkosRequest} req - Arkos request object\n * @param {ArkosResponse} res - Arkos response object\n * @param {ArkosNextFunction} next - Arkos next middleware function\n */\n updateFile = catchAsync(\n async (req: ArkosRequest, res: ArkosResponse, next: ArkosNextFunction) => {\n this.interceptors =\n getModuleComponents(\"file-upload\")?.interceptors || {};\n\n const { fileType, fileName } = req.params;\n const { format, width, height, resizeTo } = req.query;\n const options = { format, width, height, resizeTo };\n\n const {\n documentUploadService,\n fileUploadService,\n imageUploadService,\n videoUploadService,\n } = getFileUploadServices();\n\n const { fileUpload } = getArkosConfig();\n const baseUploadDir = fileUpload?.baseUploadDir || \"/uploads\";\n\n // Ensure upload directory exists\n const uploadPath = path.resolve(process.cwd(), baseUploadDir, fileType);\n try {\n await fs.promises.access(uploadPath);\n } catch (err) {\n // Create directory if it doesn't exist\n await fs.promises.mkdir(uploadPath, { recursive: true });\n }\n\n // Select the appropriate uploader service based on file type\n let uploader: FileUploadService;\n switch (fileType) {\n case \"images\":\n uploader = imageUploadService;\n break;\n case \"videos\":\n uploader = videoUploadService;\n break;\n case \"documents\":\n uploader = documentUploadService;\n break;\n case \"files\":\n uploader = fileUploadService;\n break;\n default:\n return next(new AppError(\"Invalid file type\", 400));\n }\n\n // Handle the file upload\n uploader.handleMultipleUpload()(req, res, async (err) => {\n if (err) return next(err);\n\n // Check if new file was uploaded\n if (\n !req.file &&\n (!req.files || !Array.isArray(req.files) || req.files.length === 0)\n ) {\n return next(new AppError(\"No new file uploaded\", 400));\n }\n\n // Only attempt to delete old file if fileName is specified\n if (fileName && fileName.trim() !== \"\") {\n try {\n const baseUploadRoute = fileUpload?.baseRoute || \"/api/uploads\";\n\n // Check if the URL follows the expected format\n const urlPattern = new RegExp(\n `${baseUploadRoute}/${fileType}/${fileName}`\n );\n\n const isExpectedUrlPattern = urlPattern.test(req.originalUrl);\n\n if (isExpectedUrlPattern) {\n // URL matches expected pattern, use deleteFileByUrl\n const oldFileUrl = `${req.protocol}://${req.get(\"host\")}${\n req.originalUrl\n }`;\n await uploader.deleteFileByUrl(oldFileUrl);\n } else {\n // URL doesn't match expected pattern, use deleteFileByName\n await uploader.deleteFileByName(fileName, fileType);\n }\n } catch (error) {\n // Log the error but continue with upload - old file might not exist\n console.warn(`Could not delete old file: ${fileName}`, error);\n }\n }\n\n // Process the new uploaded file(s)\n let data;\n if (req.files && Array.isArray(req.files) && req.files.length > 0) {\n if (fileType === \"images\") {\n // Process multiple image files with image transformations\n data = await Promise.all(\n req.files.map((file) => processImage(req, file.path, options))\n );\n } else {\n // Just store other file types without processing\n data = await Promise.all(\n req.files.map((file) => processFile(req, file.path))\n );\n }\n // Filter out any null values from failed processing\n data = data.filter((url) => url !== null);\n } else if (req.file) {\n // Process a single file\n if (fileType === \"images\") {\n data = await processImage(req, req.file.path, options);\n } else {\n data = await processFile(req, req.file.path);\n }\n }\n\n const jsonContent = {\n success: true,\n data,\n message: Array.isArray(data)\n ? fileName && fileName.trim() !== \"\"\n ? `File updated successfully. ${data.length} new files uploaded`\n : `${data.length} files uploaded successfully`\n : fileName && fileName.trim() !== \"\"\n ? \"File updated successfully\"\n : \"File uploaded successfully\",\n };\n\n if (this.interceptors.afterUpdateFile) {\n (res as any).originalData = jsonContent;\n req.responseData = jsonContent;\n res.locals.data = jsonContent;\n (res as any).originalStatus = 200;\n req.responseStatus = 200;\n res.locals.status = 200;\n return next();\n }\n\n res.status(200).json(jsonContent);\n });\n }\n );\n\n /**\n * Not implemented yet\n *\n * @deprecated\n */\n // streamFile = catchAsync(\n // async (req: ArkosRequest, res: ArkosResponse, _: ArkosNextFunction) => {\n // const { fileName, fileType } = req.params;\n\n // const filePath = path.join(\".\", \"uploads\", fileType, fileName);\n // try {\n // await fs.promises.access(filePath);\n // } catch (err) {\n // throw new AppError(\"File not found\", 404);\n // }\n\n // const fileStat = await fs.promises.stat(filePath);\n // const fileSize = fileStat.size;\n // const range = req.headers.range;\n\n // if (range) {\n // const [partialStart, partialEnd] = range\n // .replace(/bytes=/, \"\")\n // .split(\"-\");\n // const start = parseInt(partialStart, 10) || 0;\n // const end = partialEnd ? parseInt(partialEnd, 10) : fileSize - 1;\n\n // if (start >= fileSize || end >= fileSize) {\n // res.status(416).json({ error: \"Range Not Satisfiable\" });\n // return;\n // }\n\n // res.writeHead(206, {\n // \"Content-Range\": `bytes ${start}-${end}/${fileSize}`,\n // \"Accept-Ranges\": \"bytes\",\n // \"Content-Length\": end - start + 1,\n // \"Content-Type\": \"application/octet-stream\",\n // \"Content-Disposition\": `inline; filename=\"${fileName}\"`,\n // });\n\n // fs.createReadStream(filePath, { start, end }).pipe(res);\n // } else {\n // res.writeHead(200, {\n // \"Content-Length\": fileSize,\n // \"Content-Type\": \"application/octet-stream\",\n // \"Content-Disposition\": `inline; filename=\"${fileName}\"`,\n // });\n // fs.createReadStream(filePath).pipe(res);\n // }\n // }\n // );\n}\n\n/**\n * Controller instance responsible for handling file upload operations.\n * Manages the processing and routing of file upload requests.\n *\n * @remarks\n * This controller handles various file upload operations including validation,\n * storage, and response management.\n *\n * @instance\n * @constant\n * @see {@link https://www.arkosjs.com/docs/api-reference/file-upload-controller-object}\n */\nconst fileUploadController = new FileUploadController();\n\nexport default fileUploadController;\n"]}
|
|
@@ -35,23 +35,28 @@ function ArkosRouter() {
|
|
|
35
35
|
const method = prop;
|
|
36
36
|
route = config.route;
|
|
37
37
|
if (handlers.length > 0) {
|
|
38
|
+
handlers = handlers.map((handler) => (0, error_handler_1.catchAsync)(handler, {
|
|
39
|
+
type: handler.length > 3 ? "error" : "normal",
|
|
40
|
+
}));
|
|
38
41
|
const finalHandler = handlers[handlers.length - 1];
|
|
39
42
|
route_config_registry_1.default.register(finalHandler, config, method);
|
|
40
43
|
}
|
|
41
44
|
const arkosConfig = (0, exports_1.getArkosConfig)();
|
|
42
45
|
const validationConfig = arkosConfig.validation;
|
|
43
46
|
const authenticationConfig = arkosConfig.authentication;
|
|
47
|
+
if (validationConfig?.strict &&
|
|
48
|
+
"validation" in config &&
|
|
49
|
+
!config?.validation &&
|
|
50
|
+
config?.validation !== undefined)
|
|
51
|
+
throw Error("When using strict validation you must either pass { validation: false } in order to explicitly tell that no input will be received, or pass `undefined` for each input type e.g { validation: { query: undefined } } in order to deny the input of given request input.");
|
|
44
52
|
if (!validationConfig?.resolver && config.validation)
|
|
45
53
|
throw Error("Trying to pass validators into route config validation option without choosing a validation resolver under arkos.init({ validation: { resolver: '' } })");
|
|
46
54
|
if (!authenticationConfig?.mode && config.authentication)
|
|
47
55
|
throw Error("Trying to authenticate a route without choosing an authentication mode under arkos.init({ authentication: { mode: '' } })");
|
|
48
|
-
handlers = [
|
|
49
|
-
...(0, helpers_1.getMiddlewareStack)(config),
|
|
50
|
-
...handlers.map((handler) => (0, error_handler_1.catchAsync)(handler, {
|
|
51
|
-
type: handler.length > 3 ? "error" : "normal",
|
|
52
|
-
})),
|
|
53
|
-
];
|
|
56
|
+
handlers = [...(0, helpers_1.getMiddlewareStack)(config), ...handlers];
|
|
54
57
|
}
|
|
58
|
+
else
|
|
59
|
+
throw Error(`First argument of ArkosRouter().${prop}() must be a valid ArkosRouteConfig but recevied ${firstArg}`);
|
|
55
60
|
return originalMethod.call(target, route, ...handlers);
|
|
56
61
|
};
|
|
57
62
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/utils/arkos-router/index.ts"],"names":[],"mappings":";;;;;AAkCA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/utils/arkos-router/index.ts"],"names":[],"mappings":";;;;;AAkCA,8BA6EC;AAED,wDAkCC;AAnJD,qCAAiC;AAGjC,sFAA4D;AAC5D,oFAA0D;AAC1D,6CAAyE;AACzE,2CAA+C;AAC/C,+DAAyD;AA2BzD,SAAwB,WAAW;IACjC,MAAM,MAAM,GAAiB,IAAA,gBAAM,GAAE,CAAC;IAEtC,OAAO,IAAI,KAAK,CAAC,MAAM,EAAE;QACvB,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ;YACxB,MAAM,cAAc,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;YAE3D,MAAM,WAAW,GAAG;gBAClB,KAAK;gBACL,MAAM;gBACN,KAAK;gBACL,OAAO;gBACP,QAAQ;gBACR,KAAK;gBACL,MAAM;gBACN,OAAO;gBACP,SAAS;aACV,CAAC;YAEF,IAAI,WAAW,CAAC,QAAQ,CAAC,IAAc,CAAC,EAAE,CAAC;gBACzC,OAAO,UACL,QAAa,EACb,GAAG,QAA4D;oBAE/D,IAAI,KAAK,GAAG,QAAQ,CAAC;oBACrB,IAAI,gCAAoB,CAAC,kBAAkB,CAAC,QAAQ,CAAC,EAAE,CAAC;wBACtD,MAAM,MAAM,GAAG,QAA4B,CAAC;wBAC5C,MAAM,MAAM,GAAG,IAAc,CAAC;wBAC9B,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;wBAErB,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","sourcesContent":["import { Router } from \"express\";\nimport { IArkosRouter, ArkosRouteConfig } from \"./types\";\nimport { OpenAPIV3 } from \"openapi-types\";\nimport RouteConfigValidator from \"./route-config-validator\";\nimport RouteConfigRegistry from \"./route-config-registry\";\nimport { extractArkosRoutes, getMiddlewareStack } from \"./utils/helpers\";\nimport { getArkosConfig } from \"../../exports\";\nimport { catchAsync } from \"../../exports/error-handler\";\nimport { ArkosErrorRequestHandler, ArkosRequestHandler } from \"../../types\";\n\n/**\n * Creates an enhanced Express Router with features like OpenAPI documentation capabilities and smart data validation.\n *\n * The ArkosRouter extends the standard Express Router with the ability to\n * automatically capture OpenAPI metadata from route configurations.\n *\n * @example\n * const router = ArkosRouter();\n *\n * router.get(\n * {\n * route: \"/users/:id\",\n * openapi: {\n * summary: \"Get user by ID\",\n * tags: [\"Users\"]\n * }\n * },\n * (req, res) => { ... }\n * );\n *\n * @returns {IArkosRouter} A proxied Express Router instance with enhanced OpenAPI capabilities\n *\n * @see {@link ArkosRouteConfig} for configuration options\n */\nexport default function ArkosRouter(): IArkosRouter {\n const router: IArkosRouter = Router();\n\n return new Proxy(router, {\n get(target, prop, receiver) {\n const originalMethod = Reflect.get(target, prop, receiver);\n\n const httpMethods = [\n \"get\",\n \"post\",\n \"put\",\n \"patch\",\n \"delete\",\n \"all\",\n \"head\",\n \"trace\",\n \"options\",\n ];\n\n if (httpMethods.includes(prop as string)) {\n return function (\n firstArg: any,\n ...handlers: (ArkosRequestHandler | ArkosErrorRequestHandler)[]\n ) {\n let route = firstArg;\n if (RouteConfigValidator.isArkosRouteConfig(firstArg)) {\n const config = firstArg as ArkosRouteConfig;\n const method = prop as string;\n route = config.route;\n\n if (handlers.length > 0) {\n handlers = handlers.map((handler) =>\n catchAsync(handler, {\n type: handler.length > 3 ? \"error\" : \"normal\",\n })\n );\n\n const finalHandler = handlers[handlers.length - 1];\n RouteConfigRegistry.register(finalHandler, config, method);\n }\n\n const arkosConfig = getArkosConfig();\n const validationConfig = arkosConfig.validation;\n const authenticationConfig = arkosConfig.authentication;\n\n if (\n validationConfig?.strict &&\n \"validation\" in config &&\n !config?.validation &&\n config?.validation !== undefined\n )\n throw Error(\n \"When using strict validation you must either pass { validation: false } in order to explicitly tell that no input will be received, or pass `undefined` for each input type e.g { validation: { query: undefined } } in order to deny the input of given request input.\"\n );\n\n if (!validationConfig?.resolver && config.validation)\n throw Error(\n \"Trying to pass validators into route config validation option without choosing a validation resolver under arkos.init({ validation: { resolver: '' } })\"\n );\n\n if (!authenticationConfig?.mode && config.authentication)\n throw Error(\n \"Trying to authenticate a route without choosing an authentication mode under arkos.init({ authentication: { mode: '' } })\"\n );\n\n handlers = [...getMiddlewareStack(config), ...handlers];\n } else\n throw Error(\n `First argument of ArkosRouter().${prop as string}() must be a valid ArkosRouteConfig but recevied ${firstArg}`\n );\n\n return originalMethod.call(target, route, ...handlers);\n };\n }\n return originalMethod;\n },\n }) as IArkosRouter;\n}\n\nexport function generateOpenAPIFromApp(app: any) {\n const routes = extractArkosRoutes(app);\n const paths: Record<\n string,\n Record<string, Partial<OpenAPIV3.OperationObject>>\n > = {};\n\n routes.forEach(({ path, method, config }) => {\n if (config?.openapi === false) return;\n\n if (!paths[path]) paths[path] = {};\n\n if (typeof config?.openapi === \"boolean\") {\n config = {\n ...config,\n openapi: {},\n };\n }\n\n const openapi =\n typeof config?.openapi === \"object\" && config.openapi !== null\n ? config.openapi\n : {};\n\n paths[path][method.toLowerCase()] = {\n summary: openapi?.summary || `${method} ${path}`,\n description: openapi?.description || `${method} ${path}`,\n tags: openapi?.tags || [\"Others\"],\n operationId: `${method}:${path}`,\n ...openapi,\n };\n });\n\n return paths;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/utils/arkos-router/types/index.ts"],"names":[],"mappings":"","sourcesContent":["import { ErrorRequestHandler, IRouter, RequestHandler } from \"express\";\nimport { OpenAPIV3 } from \"openapi-types\";\nimport { ZodSchema } from \"zod\";\n\ntype MethodHandler = (\n configOrPath:
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/utils/arkos-router/types/index.ts"],"names":[],"mappings":"","sourcesContent":["import { ErrorRequestHandler, IRouter, RequestHandler } from \"express\";\nimport { OpenAPIV3 } from \"openapi-types\";\nimport { ZodSchema } from \"zod\";\n\ntype MethodHandler = (\n configOrPath: ArkosRouteConfig,\n ...handlers: (RequestHandler | ErrorRequestHandler)[]\n) => IArkosRouter;\n\nexport interface IArkosRouter\n extends Omit<\n IRouter,\n | \"get\"\n | \"post\"\n | \"put\"\n | \"patch\"\n | \"delete\"\n | \"options\"\n | \"head\"\n | \"trace\"\n | \"all\"\n > {\n get: MethodHandler;\n post: MethodHandler;\n put: MethodHandler;\n patch: MethodHandler;\n delete: MethodHandler;\n options: MethodHandler;\n head: MethodHandler;\n trace: MethodHandler;\n all: MethodHandler;\n}\n\nexport interface ArkosRouteConfig {\n route: string;\n authentication?:\n | boolean\n | {\n resource: string;\n action: string;\n rule?: any;\n };\n validation?:\n | undefined\n | {\n query?: ZodSchema | (new (...args: any[]) => object) | undefined;\n body?: ZodSchema | (new (...args: any[]) => object) | undefined;\n params?: ZodSchema | (new (...args: any[]) => object) | undefined;\n };\n openapi?: boolean | Partial<OpenAPIV3.OperationObject>;\n}\n"]}
|
|
@@ -64,8 +64,7 @@ function getMiddlewareStack(config) {
|
|
|
64
64
|
middlewares.push(services_1.authService.handleAccessControl(config.authentication.action, config.authentication.resource, {
|
|
65
65
|
[config.authentication.action]: config.authentication?.rule,
|
|
66
66
|
}));
|
|
67
|
-
|
|
68
|
-
middlewares.push((0, base_middlewares_1.validateRequestInputs)(config.validation));
|
|
67
|
+
middlewares.push((0, base_middlewares_1.validateRequestInputs)(config));
|
|
69
68
|
return middlewares;
|
|
70
69
|
}
|
|
71
70
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../src/utils/arkos-router/utils/helpers/index.ts"],"names":[],"mappings":";;;;;AAKA,gDA+DC;AAED,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../src/utils/arkos-router/utils/helpers/index.ts"],"names":[],"mappings":";;;;;AAKA,gDA+DC;AAED,gDAqBC;AA3FD,2DAA2D;AAC3D,gFAAkF;AAClF,wFAA8D;AAG9D,SAAgB,kBAAkB,CAChC,GAAQ,EACR,QAAQ,GAAG,EAAE;IAMb,MAAM,MAAM,GAIP,EAAE,CAAC;IAER,SAAS,gBAAgB,CAAC,KAAY,EAAE,MAAM,GAAG,EAAE;QACjD,KAAK,CAAC,OAAO,CAAC,CAAC,KAAU,EAAE,EAAE;YAC3B,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC;gBAChB,MAAM,QAAQ,GAAG,MAAM,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC;gBAC3C,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;gBAEjD,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE;oBACzB,MAAM,QAAQ,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC;oBACzC,IAAI,MAAoC,CAAC;oBAEzC,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;wBAC/B,MAAM,WAAW,GAAG,+BAAmB,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;wBAE5D,IAAI,WAAW,EAAE,CAAC;4BAChB,MAAM,GAAG,WAAW,CAAC;4BACrB,MAAM,CAAC,IAAI,CAAC;gCACV,IAAI,EAAE,QAAQ;gCACd,MAAM,EAAE,MAAM,CAAC,WAAW,EAAE;gCAC5B,MAAM;6BACP,CAAC,CAAC;4BAEH,MAAM;wBACR,CAAC;oBACH,CAAC;gBACH,CAAC,CAAC,CAAC;YACL,CAAC;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C,cAAc,CAAC,MAAM,EAC5B,MAAM,CAAC,cAAc,CAAC,QAAQ,EAC9B;YACE,CAAC,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,cAAc,EAAE,IAAI;SAC5D,CACF,CACF,CAAC;IAEJ,WAAW,CAAC,IAAI,CAAC,IAAA,wCAAqB,EAAC,MAAM,CAAC,CAAC,CAAC;IAEhD,OAAO,WAAW,CAAC;AACrB,CAAC","sourcesContent":["import { authService } from \"../../../../exports/services\";\nimport { validateRequestInputs } from \"../../../../modules/base/base.middlewares\";\nimport RouteConfigRegistry from \"../../route-config-registry\";\nimport { ArkosRouteConfig } from \"../../types\";\n\nexport function extractArkosRoutes(\n app: any,\n basePath = \"\"\n): Array<{\n path: string;\n method: string;\n config?: ArkosRouteConfig;\n}> {\n const routes: Array<{\n path: string;\n method: string;\n config?: ArkosRouteConfig;\n }> = [];\n\n function extractFromStack(stack: any[], prefix = \"\") {\n stack.forEach((layer: any) => {\n if (layer.route) {\n const fullPath = prefix + layer.route.path;\n const methods = Object.keys(layer.route.methods);\n\n methods.forEach((method) => {\n const handlers = layer.route.stack || [];\n let config: ArkosRouteConfig | undefined;\n\n for (const handler of handlers) {\n const foundConfig = RouteConfigRegistry.get(handler.handle);\n\n if (foundConfig) {\n config = foundConfig;\n routes.push({\n path: fullPath,\n method: method.toUpperCase(),\n config,\n });\n\n break;\n }\n }\n });\n } else if (layer.name === \"router\" && layer.handle?.stack) {\n let nestedPrefix = prefix;\n\n if (layer.regexp) {\n const match = layer.regexp\n .toString()\n .match(/\\/\\^?(\\\\\\/[^?]+|\\/[^?]+)/);\n if (match) {\n nestedPrefix = prefix + \"/\" + match[1].replace(/\\\\\\//g, \"/\");\n nestedPrefix = nestedPrefix\n .replace(/\\/\\//g, \"/\")\n .replace(/\\/$/, \"\");\n }\n }\n\n extractFromStack(layer.handle.stack, nestedPrefix);\n }\n });\n }\n\n const stack = app._router?.stack || app.stack;\n if (stack) extractFromStack(stack, basePath);\n\n return routes;\n}\n\nexport function getMiddlewareStack(config: ArkosRouteConfig) {\n const middlewares = [];\n if (config.authentication) middlewares.push(authService.authenticate);\n if (\n typeof config.authentication === \"object\" &&\n config.authentication.action &&\n config.authentication.resource\n )\n middlewares.push(\n authService.handleAccessControl(\n config.authentication.action,\n config.authentication.resource,\n {\n [config.authentication.action]: config.authentication?.rule,\n }\n )\n );\n\n middlewares.push(validateRequestInputs(config));\n\n return middlewares;\n}\n"]}
|
|
@@ -17,21 +17,22 @@ function generateRouterTemplate(options) {
|
|
|
17
17
|
: `import ${modelName.camel}Controller from "${imports?.controller ||
|
|
18
18
|
`./${modelName.kebab}.controller${ext === "js" ? "." + "js" : ""}`}"`;
|
|
19
19
|
const controllerHandlerLine = `${modelName.camel}Controller.someHandler`;
|
|
20
|
-
return `import {
|
|
21
|
-
import { authService } from 'arkos/services'
|
|
22
|
-
import { catchAsync } from 'arkos/error-handler'
|
|
20
|
+
return `import { ArkosRouter } from 'arkos'
|
|
23
21
|
${controllerImportLine}
|
|
24
22
|
${routerConfigTsTypeImport}
|
|
25
23
|
|
|
26
24
|
export const config${routerConfigTsType} = { }
|
|
27
25
|
|
|
28
|
-
const ${modelName.camel}Router =
|
|
26
|
+
const ${modelName.camel}Router = ArkosRouter()
|
|
29
27
|
|
|
30
28
|
${modelName.camel}Router.get(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
{
|
|
30
|
+
route: "/custom-endpoint",
|
|
31
|
+
authentication: { action: "CustomAction", resource: "${modelName.kebab}" },
|
|
32
|
+
validation: {},
|
|
33
|
+
openapi: {}
|
|
34
|
+
},
|
|
35
|
+
${controllerHandlerLine}
|
|
35
36
|
)
|
|
36
37
|
|
|
37
38
|
export default ${modelName.camel}Router
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router-template.js","sourceRoot":"","sources":["../../../../../../../src/utils/cli/utils/template-generator/templates/router-template.ts"],"names":[],"mappings":";;AAMA,
|
|
1
|
+
{"version":3,"file":"router-template.js","sourceRoot":"","sources":["../../../../../../../src/utils/cli/utils/template-generator/templates/router-template.ts"],"names":[],"mappings":";;AAMA,wDAgDC;AAtDD,+DAGwC;AAGxC,SAAgB,sBAAsB,CAAC,OAAwB;IAC7D,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IAEvC,IAAI,CAAC,SAAS;QACZ,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;IAEjE,MAAM,GAAG,GAAG,IAAA,iCAAoB,GAAE,CAAC;IACnC,MAAM,cAAc,GAClB,OAAO,EAAE,UAAU,IAAI,KAAK,SAAS,CAAC,KAAK,eAAe,GAAG,EAAE,CAAC;IAElE,MAAM,kBAAkB,GAAG,GAAG,KAAK,IAAI,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,EAAE,CAAC;IAChE,MAAM,wBAAwB,GAC5B,GAAG,KAAK,IAAI,CAAC,CAAC,CAAC,sCAAsC,CAAC,CAAC,CAAC,EAAE,CAAC;IAE7D,MAAM,gBAAgB,GAAG,IAAA,4BAAe,EAAC,cAAc,CAAC,CAAC;IAEzD,MAAM,oBAAoB,GAAG,gBAAgB;QAC3C,CAAC,CAAC,UAAU,SAAS,CAAC,KAAK,oBACvB,OAAO,EAAE,UAAU;YACnB,KAAK,SAAS,CAAC,KAAK,cAAc,GAAG,KAAK,IAAI,CAAC,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,EAClE,GAAG;QACL,CAAC,CAAC,UAAU,SAAS,CAAC,KAAK,oBACvB,OAAO,EAAE,UAAU;YACnB,KAAK,SAAS,CAAC,KAAK,cAAc,GAAG,KAAK,IAAI,CAAC,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,EAClE,GAAG,CAAC;IAER,MAAM,qBAAqB,GAAG,GAAG,SAAS,CAAC,KAAK,wBAAwB,CAAC;IAEzE,OAAO;EACP,oBAAoB;EACpB,wBAAwB;;qBAEL,kBAAkB;;QAE/B,SAAS,CAAC,KAAK;;EAErB,SAAS,CAAC,KAAK;;;2DAG0C,SAAS,CAAC,KAAK;;;;IAItE,qBAAqB;;;iBAGR,SAAS,CAAC,KAAK;CAC/B,CAAC;AACF,CAAC","sourcesContent":["import {\n checkFileExists,\n getUserFileExtension,\n} from \"../../../../helpers/fs.helpers\";\nimport { TemplateOptions } from \"../../template-generators\";\n\nexport function generateRouterTemplate(options: TemplateOptions): string {\n const { modelName, imports } = options;\n\n if (!modelName)\n throw new Error(\"Module name is required for router template\");\n\n const ext = getUserFileExtension();\n const controllerPath =\n imports?.controller || `./${modelName.kebab}.controller.${ext}`;\n\n const routerConfigTsType = ext === \"ts\" ? \": RouterConfig\" : \"\";\n const routerConfigTsTypeImport =\n ext === \"ts\" ? \"import { RouterConfig } from 'arkos'\" : \"\";\n\n const controllerExists = checkFileExists(controllerPath);\n\n const controllerImportLine = controllerExists\n ? `import ${modelName.camel}Controller from \"${\n imports?.controller ||\n `./${modelName.kebab}.controller${ext === \"js\" ? \".\" + \"js\" : \"\"}`\n }\"`\n : `import ${modelName.camel}Controller from \"${\n imports?.controller ||\n `./${modelName.kebab}.controller${ext === \"js\" ? \".\" + \"js\" : \"\"}`\n }\"`;\n\n const controllerHandlerLine = `${modelName.camel}Controller.someHandler`;\n\n return `import { ArkosRouter } from 'arkos'\n${controllerImportLine}\n${routerConfigTsTypeImport}\n\nexport const config${routerConfigTsType} = { }\n\nconst ${modelName.camel}Router = ArkosRouter()\n\n${modelName.camel}Router.get(\n {\n route: \"/custom-endpoint\",\n authentication: { action: \"CustomAction\", resource: \"${modelName.kebab}\" },\n validation: {},\n openapi: {}\n },\n ${controllerHandlerLine}\n)\n\nexport default ${modelName.camel}Router\n`;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/exports/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,WAAW,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/exports/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,WAAW,MAAM,uBAAuB,CAAC;AAgBhD,MAAM,KAAK,GAAG;IACZ,IAAI,EAAE,OAAO;CACd,CAAC;AAEF,OAAO,EAKL,cAAc,EAGd,cAAc,EAyBd,WAAW,GAEZ,CAAC;AAOF,eAAe,KAAK,CAAC","sourcesContent":["import { RouterConfig } from \"../types/router-config\";\nimport { BaseController } from \"./../modules/base/base.controller\";\nimport { initApp, getArkosConfig } from \"../server\";\nimport ArkosRouter from \"../utils/arkos-router\";\nimport { ArkosRouteConfig } from \"../utils/arkos-router/types\";\nimport {\n ArkosRequest,\n ArkosResponse,\n ArkosNextFunction,\n ArkosRequestHandler,\n} from \"../types\";\nimport { ArkosConfig } from \"../types/arkos-config\";\n\n/**\n * Initializes the Arkos application.\n *\n * @module arkos\n * @property {Function} init - Function to initialize the app.\n */\nconst arkos = {\n init: initApp,\n};\n\nexport {\n ArkosRequest,\n ArkosResponse,\n ArkosNextFunction,\n ArkosRequestHandler,\n BaseController,\n ArkosConfig,\n RouterConfig,\n getArkosConfig,\n /**\n * Creates an enhanced Express Router with features like OpenAPI documentation capabilities and smart data validation.\n *\n * The ArkosRouter extends the standard Express Router with the ability to\n * automatically capture OpenAPI metadata from route configurations.\n *\n * @example\n * const router = ArkosRouter();\n *\n * router.get(\n * {\n * route: \"/users/:id\",\n * openapi: {\n * summary: \"Get user by ID\",\n * tags: [\"Users\"]\n * }\n * },\n * (req, res) => { ... }\n * );\n *\n * @returns {IArkosRouter} A proxied Express Router instance with enhanced OpenAPI capabilities\n *\n * @see {@link ArkosRouteConfig} for configuration options\n */\n ArkosRouter,\n ArkosRouteConfig,\n};\n\n/**\n * Main entry point for the Arkos module.\n *\n * @module arkos\n */\nexport default arkos;\n"]}
|
|
@@ -19,8 +19,10 @@ export const authControllerFactory = async (interceptors = {}) => {
|
|
|
19
19
|
delete user[key];
|
|
20
20
|
});
|
|
21
21
|
if (interceptors?.afterGetMe) {
|
|
22
|
+
res.originalData = { data: user };
|
|
22
23
|
req.responseData = { data: user };
|
|
23
24
|
res.locals.data = { data: user };
|
|
25
|
+
res.originalStatus = 200;
|
|
24
26
|
req.responseStatus = 200;
|
|
25
27
|
res.locals.status = 200;
|
|
26
28
|
return next();
|
|
@@ -36,8 +38,10 @@ export const authControllerFactory = async (interceptors = {}) => {
|
|
|
36
38
|
delete user[key];
|
|
37
39
|
});
|
|
38
40
|
if (interceptors?.afterUpdateMe) {
|
|
41
|
+
res.originalData = { data: user };
|
|
39
42
|
req.responseData = { data: user };
|
|
40
43
|
res.locals.data = { data: user };
|
|
44
|
+
res.originalStatus = 200;
|
|
41
45
|
req.responseStatus = 200;
|
|
42
46
|
res.locals.status = 200;
|
|
43
47
|
return next();
|
|
@@ -50,8 +54,10 @@ export const authControllerFactory = async (interceptors = {}) => {
|
|
|
50
54
|
httpOnly: true,
|
|
51
55
|
});
|
|
52
56
|
if (interceptors?.afterLogout) {
|
|
57
|
+
res.originalData = null;
|
|
53
58
|
req.responseData = null;
|
|
54
59
|
res.locals.data = null;
|
|
60
|
+
res.originalStatus = 204;
|
|
55
61
|
req.responseStatus = 204;
|
|
56
62
|
res.locals.status = 204;
|
|
57
63
|
return next();
|
|
@@ -111,8 +117,10 @@ export const authControllerFactory = async (interceptors = {}) => {
|
|
|
111
117
|
res.cookie("arkos_access_token", token, cookieOptions);
|
|
112
118
|
req.accessToken = token;
|
|
113
119
|
if (interceptors?.afterLogin) {
|
|
120
|
+
res.originalData = req.responseData;
|
|
114
121
|
req.additionalData = { user };
|
|
115
122
|
res.locals.additional = { user };
|
|
123
|
+
res.originalStatus = 200;
|
|
116
124
|
req.responseStatus = 200;
|
|
117
125
|
res.locals.status = 200;
|
|
118
126
|
return next();
|
|
@@ -130,8 +138,10 @@ export const authControllerFactory = async (interceptors = {}) => {
|
|
|
130
138
|
signup: catchAsync(async (req, res, next) => {
|
|
131
139
|
const user = (await userService.createOne(req.body, req.prismaQueryOptions || {}));
|
|
132
140
|
if (interceptors?.afterSignup) {
|
|
141
|
+
res.originalData = { data: user };
|
|
133
142
|
req.responseData = { data: user };
|
|
134
143
|
res.locals.data = { data: user };
|
|
144
|
+
res.originalStatus = 201;
|
|
135
145
|
req.responseStatus = 201;
|
|
136
146
|
res.locals.status = 201;
|
|
137
147
|
return next();
|
|
@@ -147,8 +157,10 @@ export const authControllerFactory = async (interceptors = {}) => {
|
|
|
147
157
|
deletedSelfAccountAt: new Date().toISOString(),
|
|
148
158
|
}, req.prismaQueryOptions || {}));
|
|
149
159
|
if (interceptors?.afterDeleteMe) {
|
|
160
|
+
res.originalData = { data: updatedUser };
|
|
150
161
|
req.responseData = { data: updatedUser };
|
|
151
162
|
res.locals.data = { data: updatedUser };
|
|
163
|
+
res.originalStatus = 200;
|
|
152
164
|
req.responseStatus = 200;
|
|
153
165
|
res.locals.status = 200;
|
|
154
166
|
return next();
|
|
@@ -181,26 +193,21 @@ export const authControllerFactory = async (interceptors = {}) => {
|
|
|
181
193
|
password: await authService.hashPassword(newPassword),
|
|
182
194
|
passwordChangedAt: new Date(Date.now()),
|
|
183
195
|
});
|
|
196
|
+
const responseData = {
|
|
197
|
+
status: "success",
|
|
198
|
+
message: "Password updated successfully!",
|
|
199
|
+
};
|
|
184
200
|
if (interceptors?.afterUpdatePassword) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
message: "Password updated successfully!",
|
|
191
|
-
};
|
|
192
|
-
res.locals.data = {
|
|
193
|
-
status: "success",
|
|
194
|
-
message: "Password updated successfully!",
|
|
195
|
-
};
|
|
201
|
+
res.originalData = responseData;
|
|
202
|
+
req.additionalData = { user };
|
|
203
|
+
req.responseData = responseData;
|
|
204
|
+
res.locals.data = responseData;
|
|
205
|
+
res.originalStatus = 200;
|
|
196
206
|
req.responseStatus = 200;
|
|
197
207
|
res.locals.status = 200;
|
|
198
208
|
return next();
|
|
199
209
|
}
|
|
200
|
-
res.status(200).json(
|
|
201
|
-
status: "success",
|
|
202
|
-
message: "Password updated successfully!",
|
|
203
|
-
});
|
|
210
|
+
res.status(200).json(responseData);
|
|
204
211
|
}),
|
|
205
212
|
findManyAuthAction: catchAsync(async (_, res) => {
|
|
206
213
|
const arkosConfig = getArkosConfig();
|