Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ export type MiddlewareConfiguration = {
module: string,
middlewareFactoryName: string,
middlewareName: string,
configName: string,
matcherName: string,
},
};

Expand All @@ -48,37 +46,45 @@ export class NextJsMiddlewareCodemod implements Codemod<t.File> {
public apply(input: t.File): Promise<ResultCode<t.File>> {
const {body} = input.program;

const isConfigReexported = hasReexport(input, {
moduleName: this.configuration.import.module,
importName: this.configuration.import.configName,
});

const isMiddlewareReexported = hasReexport(input, {
moduleName: this.configuration.import.module,
importName: this.configuration.import.middlewareName,
});

const localConfig = isConfigReexported ? null : NextJsMiddlewareCodemod.findConfig(input);
const localConfig = NextJsMiddlewareCodemod.findConfig(input);

const addConfigExport = (): void => {
// Add a configuration object with the matcher pattern
body.push(t.exportNamedDeclaration(
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('config'),
t.objectExpression([
t.objectProperty(
t.identifier('matcher'),
t.stringLiteral(this.configuration.matcherPattern),
),
]),
),
]),
));
};

if (isMiddlewareReexported) {
if (isConfigReexported || localConfig !== null) {
// Middleware is re-exported and config is re-exported or found in the source code,
if (localConfig !== null) {
// Middleware is re-exported and config found in the source code,
// consider the middleware configured
return Promise.resolve({
modified: false,
result: input,
});
}

// No config found in the source code, add a re-export
const modified = addReexport(input, {
type: 'value',
moduleName: this.configuration.import.module,
importName: this.configuration.import.configName,
});
// Add a configuration object with the matcher pattern
addConfigExport();

return Promise.resolve({
modified: modified,
modified: true,
result: input,
});
}
Expand Down Expand Up @@ -120,21 +126,17 @@ export class NextJsMiddlewareCodemod implements Codemod<t.File> {
if (middlewareNode === null) {
if (localConfig === null) {
// No middleware found or configuration object,
// just add re-exports
const middlewareReexport = addReexport(input, {
// just add middleware re-export
addReexport(input, {
type: 'value',
moduleName: this.configuration.import.module,
importName: this.configuration.import.middlewareName,
});

const configReexport = addReexport(input, {
type: 'value',
moduleName: this.configuration.import.module,
importName: this.configuration.import.configName,
});
addConfigExport();

return Promise.resolve({
modified: middlewareReexport || configReexport,
modified: true,
result: input,
});
}
Expand All @@ -149,7 +151,7 @@ export class NextJsMiddlewareCodemod implements Codemod<t.File> {
t.identifier('matcher'),
t.memberExpression(
t.identifier(localConfig.name),
t.identifier(this.configuration.import.matcherName),
t.identifier('matcher'),
),
),
]),
Expand Down
2 changes: 0 additions & 2 deletions src/infrastructure/application/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1793,8 +1793,6 @@ export class Cli {
module: '@croct/plug-next/middleware',
middlewareName: 'middleware',
middlewareFactoryName: 'withCroct',
configName: 'config',
matcherName: 'matcher',
},
}),
}),
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,11 @@ exports[`NextJsMiddlewareCodemod should correctly transform defaultExportClass.t
// invalid
}

export { middleware, config } from "@croct/plug-next/middleware";
export { middleware } from "@croct/plug-next/middleware";

export const config = {
matcher: "/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)"
};
"
`;

Expand Down Expand Up @@ -319,7 +323,13 @@ export default something;
"
`;

exports[`NextJsMiddlewareCodemod should correctly transform empty.ts: empty.ts 1`] = `"export { middleware, config } from "@croct/plug-next/middleware";"`;
exports[`NextJsMiddlewareCodemod should correctly transform empty.ts: empty.ts 1`] = `
"export { middleware } from "@croct/plug-next/middleware";

export const config = {
matcher: "/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)"
};"
`;

exports[`NextJsMiddlewareCodemod should correctly transform existingAliasedHofCall.ts: existingAliasedHofCall.ts 1`] = `
"import { withCroct as croctMiddleware } from "@croct/plug-next/middleware";
Expand Down Expand Up @@ -381,11 +391,6 @@ export default withCroct({
"
`;

exports[`NextJsMiddlewareCodemod should correctly transform existingConfigReexport.ts: existingConfigReexport.ts 1`] = `
"export { config, middleware } from "@croct/plug-next/middleware";
"
`;

exports[`NextJsMiddlewareCodemod should correctly transform existingHofCall.ts: existingHofCall.ts 1`] = `
"import { withCroct } from "@croct/plug-next/middleware";

Expand Down Expand Up @@ -435,20 +440,6 @@ export const middleware = croctMiddleware({
"
`;

exports[`NextJsMiddlewareCodemod should correctly transform existingLocalConfigAndMiddlewareReexport.ts: existingLocalConfigAndMiddlewareReexport.ts 1`] = `
"export { middleware } from "@croct/plug-next/middleware";

export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
}
"
`;

exports[`NextJsMiddlewareCodemod should correctly transform existingMiddlewareAndConfigReexport.ts: existingMiddlewareAndConfigReexport.ts 1`] = `
"export { middleware, config } from "@croct/plug-next/middleware";
"
`;

exports[`NextJsMiddlewareCodemod should correctly transform existingMiddlewareCall.ts: existingMiddlewareCall.ts 1`] = `
"import { middleware as croctMiddleware } from "@croct/plug-next/middleware";

Expand All @@ -459,7 +450,11 @@ export default croctMiddleware(function () {
`;

exports[`NextJsMiddlewareCodemod should correctly transform existingMiddlewareReexport.ts: existingMiddlewareReexport.ts 1`] = `
"export { middleware, config } from "@croct/plug-next/middleware";
"export { middleware } from "@croct/plug-next/middleware";

export const config = {
matcher: "/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)"
};
"
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ describe('NextJsMiddlewareCodemod', () => {
module: '@croct/plug-next/middleware',
middlewareFactoryName: 'withCroct',
middlewareName: 'middleware',
matcherName: 'matcher',
configName: 'config',
},
};

Expand Down