Skip to content

Commit 0719252

Browse files
committed
add cache options for next.js
1 parent 5841257 commit 0719252

File tree

7 files changed

+27
-10
lines changed

7 files changed

+27
-10
lines changed

src/core/renderers/declaration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ type DeclarationTemplate = {
99
operations: DeclaredOperation[],
1010
};
1111

12-
export default function generateDeclaration(paths: OpenAPI["paths"]) {
12+
export default function generateDeclaration(paths: OpenAPI["paths"], framework: string | null) {
1313
const template = getTemplate<DeclarationTemplate>(declarationTemplate);
1414
return template({
1515
importedSchemas: resolveSchemas(paths),
16-
operations: resolveDeclaredOperations(paths),
16+
operations: resolveDeclaredOperations(paths, framework),
1717
});
1818
}

src/core/resolvers/declared-operation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ function resolveOperationResult(responses: Operation["responses"]) {
1717
return schemas.find(s => typeof s === "string") ?? "unknown";
1818
}
1919

20-
export function resolveDeclaredOperations(paths: OpenAPI["paths"]) {
21-
return resolveEndpoints(paths).map<DeclaredOperation>(({ operation }) => ({
20+
export function resolveDeclaredOperations(paths: OpenAPI["paths"], framework: string | null) {
21+
return resolveEndpoints(paths).map<DeclaredOperation>(({ method, operation }) => ({
2222
name: operation.operationId,
23-
parameters: resolveOperationParams(operation, true).join(", "),
23+
parameters: resolveOperationParams(operation, method, true, framework).join(", "),
2424
result: resolveOperationResult(operation.responses),
2525
}));
2626
}

src/core/resolvers/operation-param.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function sortRequiredParamsFirst(paramA: OperationParameter, paramB: OperationPa
2323
return paramA.required ? -1 : 1;
2424
}
2525

26-
export function resolveOperationParams(operation: Operation, typescript: boolean) {
26+
export function resolveOperationParams(operation: Operation, method: string, typescript: boolean, framework: string | null) {
2727
const resolvedParams = (operation.parameters ?? [])
2828
.filter(param => param.in === "path" || param.in === "query")
2929
.toSorted(sortRequiredParamsFirst)
@@ -39,5 +39,8 @@ export function resolveOperationParams(operation: Operation, typescript: boolean
3939
collection.push(resolveRequestBody(operation.requestBody, typescript));
4040
}
4141
}
42+
if (framework === "next" && method.toUpperCase() === "GET") {
43+
collection.unshift(typescript ? "cacheTag: string | null" : "cacheTag");
44+
}
4245
return collection;
4346
}

src/core/resolvers/operation.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export type OperationTemplate = {
1212
searchParams: OperationParameter[],
1313
hasFormData: boolean,
1414
responses: string[],
15+
isCacheable: boolean,
1516
};
1617

1718
function quotePathName(pathName: string, parameters: OperationParameter[]) {
@@ -24,14 +25,15 @@ function getSearchParams(parameters: OperationParameter[]) {
2425
return parameters.filter(param => param.in === "query");
2526
}
2627

27-
export default function resolveOperations(paths: OpenAPI["paths"]) {
28+
export default function resolveOperations(paths: OpenAPI["paths"], framework: string | null) {
2829
return resolveEndpoints(paths).map<OperationTemplate>(({ method, path, operation }) => ({
2930
name: operation.operationId,
3031
method: method.toUpperCase(),
3132
path: quotePathName(path, operation.parameters ?? []),
32-
parameters: resolveOperationParams(operation, false).join(", "),
33+
parameters: resolveOperationParams(operation, method, false, framework).join(", "),
3334
searchParams: getSearchParams(operation.parameters ?? []),
3435
hasFormData: hasFormData(operation),
3536
responses: resolveResponses(operation.responses),
37+
isCacheable: framework === "next" && method.toUpperCase() === "GET",
3638
}));
3739
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ import generateSwaggerJson from "./core/swagger";
4141
const serviceName = capitalize(appName.replace(/-/g, " "));
4242
const envName = `${appName.replace(/-/g, "_").toUpperCase()}_BASE_URL`;
4343

44-
const resolvedPaths = resolveOperations(data.paths);
44+
const resolvedPaths = resolveOperations(data.paths, framework);
4545

4646
const content = generateInterface(envName, resolvedPaths);
4747
await createFile(content, "index.js", outputDir, "dist");
4848

49-
const declaration = generateDeclaration(data.paths);
49+
const declaration = generateDeclaration(data.paths, framework);
5050
await createFile(declaration, "index.d.ts", outputDir, "dist");
5151

5252
const doc = generateDocumentation(serviceName, packageName, envName, data.paths);

src/templates/operation-with-form-data.hbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export async function {{name}}({{parameters}}) {
1010
const response = await fetch(url, {
1111
method: "{{method}}",
1212
body: formData,
13+
{{#if isCacheable}}
14+
cache: cacheTag ? "force-cache" : "no-store",
15+
next: {
16+
tags: cacheTag ? [cacheTag] : undefined,
17+
}
18+
{{/if}}
1319
});
1420
switch (response.status) {
1521
{{#each responses}}

src/templates/operation.hbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ export async function {{name}}({{parameters}}) {
88
{{#if (hasRequestBody parameters)}}
99
body: JSON.stringify(requestBody),
1010
{{/if}}
11+
{{#if isCacheable}}
12+
cache: cacheTag ? "force-cache" : "no-store",
13+
next: {
14+
tags: cacheTag ? [cacheTag] : undefined,
15+
}
16+
{{/if}}
1117
});
1218
switch (response.status) {
1319
{{#each responses}}

0 commit comments

Comments
 (0)