@scalar/nextjs-openapi 0.3.56 → 0.3.58

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @scalar/nextjs-openapi
2
2
 
3
+ ## 0.3.58
4
+
5
+ No changes in this release.
6
+
7
+ ## 0.3.57
8
+
9
+ ### Patch Changes
10
+
11
+ - [#10898](https://github.com/scalar/scalar/pull/10898): Preserve path parameters when generating OpenAPI descriptions from Next.js handlers with asynchronous params, and mark path parameters as required.
12
+
13
+ Infer missing path parameters from route paths when handlers use named parameter aliases or generated Next.js RouteContext types.
14
+
3
15
  ## 0.3.56
4
16
 
5
17
  ## 0.3.55
@@ -1 +1 @@
1
- {"version":3,"file":"openapi.d.ts","sourceRoot":"","sources":["../src/openapi.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAA;AAE5E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAa9C;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAAA;AA+BtC;;;;;;;;;GASG;AACH,eAAO,MAAM,OAAO,GAAI,SAAQ,aAAkB;eAoC7B,WAAW;CAe/B,CAAA"}
1
+ {"version":3,"file":"openapi.d.ts","sourceRoot":"","sources":["../src/openapi.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAA;AAE5E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAa9C;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAAA;AA+BtC;;;;;;;;;GASG;AACH,eAAO,MAAM,OAAO,GAAI,SAAQ,aAAkB;eAkC7B,WAAW;CAe/B,CAAA"}
package/dist/openapi.js CHANGED
@@ -52,7 +52,6 @@ export const OpenAPI = (config = {}) => {
52
52
  routeFileNames.forEach((fileName) => {
53
53
  const sourceFile = program.getSourceFile(fileName);
54
54
  if (sourceFile) {
55
- const resp = getPathSchema(sourceFile, program);
56
55
  // Grab the path from the fileName
57
56
  const rawPath = fileName
58
57
  .replace(/^(?:src\/)?app|\/route\.ts$/g, '')
@@ -60,7 +59,7 @@ export const OpenAPI = (config = {}) => {
60
59
  .replace(/\/\(\w+\)/g, '')
61
60
  .replace(/]/g, '}');
62
61
  const path = rawPath.startsWith('/') ? rawPath : '/' + rawPath;
63
- spec.paths[path] = resp;
62
+ spec.paths[path] = getPathSchema(sourceFile, program, path);
64
63
  }
65
64
  });
66
65
  return {
package/dist/path.d.ts CHANGED
@@ -3,5 +3,5 @@ import { type Program, type SourceFile } from 'typescript';
3
3
  /**
4
4
  * Traverse the typescript file and extract as much info as we can for the openapi spec
5
5
  */
6
- export declare const getPathSchema: (sourceFile: SourceFile, program: Program) => OpenAPIV3_1.PathsObject;
6
+ export declare const getPathSchema: (sourceFile: SourceFile, program: Program, routePath?: string) => OpenAPIV3_1.PathsObject;
7
7
  //# sourceMappingURL=path.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"path.d.ts","sourceRoot":"","sources":["../src/path.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAChD,OAAO,EAGL,KAAK,OAAO,EACZ,KAAK,UAAU,EAShB,MAAM,YAAY,CAAA;AAoDnB;;GAEG;AACH,eAAO,MAAM,aAAa,GAAI,YAAY,UAAU,EAAE,SAAS,OAAO,KAAG,WAAW,CAAC,WA8CpF,CAAA"}
1
+ {"version":3,"file":"path.d.ts","sourceRoot":"","sources":["../src/path.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAChD,OAAO,EAGL,KAAK,OAAO,EACZ,KAAK,UAAU,EAWhB,MAAM,YAAY,CAAA;AAwEnB;;GAEG;AACH,eAAO,MAAM,aAAa,GACxB,YAAY,UAAU,EACtB,SAAS,OAAO,EAChB,YAAY,MAAM,KACjB,WAAW,CAAC,WAmEd,CAAA"}
package/dist/path.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { extname, join } from 'node:path';
2
2
  import { generateResponses, getJSDocFromNode, getSchemaFromTypeNode } from '@scalar/ts-to-openapi';
3
- import { isArrowFunction, isFunctionDeclaration, isFunctionExpression, isIdentifier, isParameter, isPropertySignature, isTypeLiteralNode, isVariableStatement, } from 'typescript';
3
+ import { isArrowFunction, isFunctionDeclaration, isFunctionExpression, isIdentifier, isParameter, isPropertySignature, isTypeLiteralNode, isTypeReferenceNode, isVariableStatement, } from 'typescript';
4
4
  /** Check if identifier is a supported http method */
5
5
  const checkForMethod = (identifier) => {
6
6
  const method = identifier?.escapedText?.toLowerCase();
@@ -13,6 +13,16 @@ const fileNameResolver = (source, target) => {
13
13
  const targetPath = join(source.replace(/\/([^/]+)$/, ''), targetRelative);
14
14
  return targetPath;
15
15
  };
16
+ /** Next.js 16 requires asynchronous route params; older handlers use plain objects. */
17
+ const unwrapPromise = (typeNode) => {
18
+ if (isTypeReferenceNode(typeNode) &&
19
+ isIdentifier(typeNode.typeName) &&
20
+ typeNode.typeName.text === 'Promise' &&
21
+ typeNode.typeArguments?.length === 1) {
22
+ return typeNode.typeArguments[0];
23
+ }
24
+ return typeNode;
25
+ };
16
26
  /**
17
27
  * Takes a parameter node and returns a path parameter schema
18
28
  */
@@ -26,9 +36,12 @@ const extractPathParams = (node, program) => {
26
36
  isPropertySignature(node.type.members[0]) &&
27
37
  isIdentifier(node.type.members[0].name) &&
28
38
  node.type.members[0].name.escapedText === 'params' &&
29
- node.type.members[0].type &&
30
- isTypeLiteralNode(node.type.members[0].type)) {
31
- return node.type.members[0].type?.members.flatMap((member) => {
39
+ node.type.members[0].type) {
40
+ const resolvedType = unwrapPromise(node.type.members[0].type);
41
+ if (!resolvedType || !isTypeLiteralNode(resolvedType)) {
42
+ return [];
43
+ }
44
+ return resolvedType.members.flatMap((member) => {
32
45
  if (!isPropertySignature(member) || !member.type) {
33
46
  return [];
34
47
  }
@@ -36,6 +49,7 @@ const extractPathParams = (node, program) => {
36
49
  name: member.name?.getText(),
37
50
  schema: getSchemaFromTypeNode(member.type, program, fileNameResolver),
38
51
  in: 'path',
52
+ required: true,
39
53
  };
40
54
  });
41
55
  }
@@ -44,7 +58,7 @@ const extractPathParams = (node, program) => {
44
58
  /**
45
59
  * Traverse the typescript file and extract as much info as we can for the openapi spec
46
60
  */
47
- export const getPathSchema = (sourceFile, program) => {
61
+ export const getPathSchema = (sourceFile, program, routePath) => {
48
62
  const path = {};
49
63
  const typeChecker = program.getTypeChecker();
50
64
  sourceFile.statements.forEach((statement) => {
@@ -82,5 +96,22 @@ export const getPathSchema = (sourceFile, program) => {
82
96
  }
83
97
  }
84
98
  });
99
+ // Named aliases and generated RouteContext types may not be available to the
100
+ // compiler host. The route still provides the names required by OpenAPI.
101
+ if (routePath) {
102
+ const parameterNames = [...new Set([...routePath.matchAll(/\{([^{}]+)\}/g)].map((match) => match[1]))];
103
+ for (const operation of Object.values(path)) {
104
+ if (!operation) {
105
+ continue;
106
+ }
107
+ const parameters = operation.parameters ?? [];
108
+ for (const name of parameterNames) {
109
+ if (!parameters.some((parameter) => 'name' in parameter && parameter.in === 'path' && parameter.name === name)) {
110
+ parameters.push({ name, in: 'path', required: true, schema: { type: 'string' } });
111
+ }
112
+ }
113
+ operation.parameters = parameters;
114
+ }
115
+ }
85
116
  return path;
86
117
  };
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  "scalar",
17
17
  "references"
18
18
  ],
19
- "version": "0.3.56",
19
+ "version": "0.3.58",
20
20
  "engines": {
21
21
  "node": ">=22"
22
22
  },
@@ -33,21 +33,23 @@
33
33
  "CHANGELOG.md"
34
34
  ],
35
35
  "dependencies": {
36
- "@scalar/nextjs-api-reference": "0.12.16",
36
+ "@scalar/nextjs-api-reference": "0.12.18",
37
37
  "@scalar/ts-to-openapi": "0.2.5",
38
- "@scalar/types": "0.27.0",
38
+ "@scalar/types": "0.28.1",
39
39
  "fast-glob": "^3.3.3"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/node": "^24.19.2",
43
43
  "@types/react": "^19.3.0",
44
44
  "@types/react-dom": "^19.3.0",
45
- "next": "^15.5.28",
46
- "openapi-types": "^12.1.3"
45
+ "next": "^16.4.3",
46
+ "openapi-types": "^12.1.3",
47
+ "vitest": "5.0.5"
47
48
  },
48
49
  "scripts": {
49
50
  "build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
50
51
  "dev": "next dev playground -p 5066",
52
+ "test": "vitest --run",
51
53
  "types:check": "tsgo --noEmit"
52
54
  }
53
55
  }