Skip to content

Commit

Permalink
Use dynamic route for the API route instrumentation
Browse files Browse the repository at this point in the history
This fixes an issue that was caused by a forgotten
hardcoded value for the URL which will be used for
fetching the configuration for an individual endpoint.
  • Loading branch information
blomqma committed Dec 9, 2022
1 parent a309e05 commit f84af1a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
7 changes: 1 addition & 6 deletions packages/next-rest-framework/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { defineCatchAllHandler } from './define-catch-all-handler';
import { defineEndpoints } from './define-endpoints';
import { NextRestFrameworkConfig } from './types';
import merge from 'lodash.merge';
import {
getDefaultConfig,
getOpenApiSpecWithPaths,
logInitInfo
} from './utils';
import { getDefaultConfig, logInitInfo } from './utils';

export const NextRestFramework = <GlobalMiddlewareResponse>(
_config?: NextRestFrameworkConfig<GlobalMiddlewareResponse>
Expand All @@ -19,7 +15,6 @@ export const NextRestFramework = <GlobalMiddlewareResponse>(

return {
config,
getOpenApiSpec: async () => await getOpenApiSpecWithPaths({ config }),
defineCatchAllHandler: defineCatchAllHandler({ config }),
defineEndpoints: defineEndpoints({ config })
};
Expand Down
2 changes: 1 addition & 1 deletion packages/next-rest-framework/src/define-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export const defineEndpoints = <GlobalMiddlewareResponse>({
[openApiJsonPath, openApiYamlPath, swaggerUiPath].includes(url) &&
exposeOpenApiSpec
) {
const spec = await getOpenApiSpecWithPaths({ config });
const spec = await getOpenApiSpecWithPaths({ req, res, config });

if (_warnAboutReservedPaths) {
handleReservedPathWarnings({ url, config });
Expand Down
22 changes: 17 additions & 5 deletions packages/next-rest-framework/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Modify } from './utility-types';
import isEqualWith from 'lodash.isequalwith';
import zodToJsonSchema from 'zod-to-json-schema';
import yupToJsonSchema from '@sodaru/yup-to-json-schema';
import { NextApiRequest, NextApiResponse } from 'next';

const logNextRestFrameworkError = ({ error }: { error: unknown }) => {
if (process.env.NODE_ENV !== 'production') {
Expand Down Expand Up @@ -259,11 +260,15 @@ export const isValidMethod = (x: unknown): x is ValidMethod =>
Object.values(ValidMethod).includes(x as ValidMethod);

export const getOpenApiSpecWithPaths = async ({
req,
res,
config
}: {
req: NextApiRequest;
res: NextApiResponse;
config: NextRestFrameworkConfig;
}) => {
const paths = await generatePaths({ config });
const paths = await generatePaths({ req, res, config });

const spec = {
...config.openApiSpec,
Expand Down Expand Up @@ -442,10 +447,13 @@ export const getPathsFromMethodHandlers = ({
return paths;
};

export const generatePaths = async <GlobalMiddlewareResponse>({
config: { openApiJsonPath, openApiYamlPath, swaggerUiPath, errorHandler }
export const generatePaths = async ({
req: { headers },
config: { openApiJsonPath, openApiYamlPath, swaggerUiPath }
}: {
config: NextRestFrameworkConfig<GlobalMiddlewareResponse>;
req: NextApiRequest;
res: NextApiResponse;
config: NextRestFrameworkConfig;
}): Promise<OpenAPIV3_1.PathsObject> => {
const filterApiRoutes = (file: string) => {
const isCatchAllRoute = file.includes('...');
Expand Down Expand Up @@ -489,7 +497,11 @@ export const generatePaths = async <GlobalMiddlewareResponse>({
await Promise.all(
mapApiRoutes.map(async (route) => {
try {
const res = await fetch(`http://localhost:3000${route}`, {
const proto = headers['x-forwarded-proto'] ?? 'http';
const host = headers.host;
const url = `${proto}://${host}${route}`;

const res = await fetch(url, {
headers: {
'User-Agent': NEXT_REST_FRAMEWORK_USER_AGENT
}
Expand Down

0 comments on commit f84af1a

Please sign in to comment.