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
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project>

<PropertyGroup>
<VersionPrefix>3.8.0</VersionPrefix>
<PackageValidationBaselineVersion>3.7.0</PackageValidationBaselineVersion>
<VersionPrefix>3.9.0</VersionPrefix>
<PackageValidationBaselineVersion>3.8.0</PackageValidationBaselineVersion>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
4 changes: 4 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

These are the NuGet package releases. See also [npm Release Notes](ReleaseNotesNpm.md).

## 3.9.0

* Add ability to configure route specific `bodyLimit` in fastify plugin, via the new `routeOptions` property on the plugin constructor.

## 3.8.0

* Set explicit Content-Type headers in fastify plugin routes.
Expand Down
8 changes: 8 additions & 0 deletions conformance/src/fastify/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ const app: FastifyPluginAsync<FastifyServerOptions> = async (fastify): Promise<v
new ConformanceApiService(conformanceTestsJson.tests),
caseInsensitiveQueryStringKeys: true,
includeErrorDetails: true,
routeOptions: {
'*': {
bodyLimit: 10 * 1024, // 10 KB
},
'createWidget': {
bodyLimit: 100 * 1024, // 100 KB
}
}
};

fastify.register(conformanceApiPlugin, conformanceApiPluginOptions);
Expand Down
54 changes: 38 additions & 16 deletions conformance/src/fastify/conformanceApiPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ export type ConformanceApiPluginOptions = fastifyTypes.RegisterOptions & {

/** Whether to include error details in the response. Defaults to false. */
includeErrorDetails?: boolean;

/** Additional supported route options to use with routes. Only `bodyLimit` is currently supported. Use '*' to apply to all routes. */
routeOptions?: { [K in '*' | ConformanceApiRoutes]?: Pick<fastifyTypes.RouteOptions, 'bodyLimit'> };
}

export type ConformanceApiRoutes = 'getApiInfo' | 'getWidgets' | 'createWidget' | 'getWidget' | 'deleteWidget' | 'getWidgetBatch' | 'mirrorFields' | 'checkQuery' | 'checkPath' | 'mirrorHeaders' | 'mixed' | 'required' | 'mirrorBytes' | 'mirrorText' | 'bodyTypes';

/** EXPERIMENTAL: The generated code for this plugin is subject to change/removal without a major version bump. */
export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceApiPluginOptions> = async (fastify, opts) => {
const { serviceOrFactory, caseInsensitiveQueryStringKeys, includeErrorDetails } = opts;
const { serviceOrFactory, caseInsensitiveQueryStringKeys, includeErrorDetails, routeOptions } = opts;

const getService = typeof serviceOrFactory === 'function' ? serviceOrFactory : () => serviceOrFactory;

Expand Down Expand Up @@ -73,9 +78,12 @@ export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceAp
});
}

const defaultBodyLimit = routeOptions?.['*']?.bodyLimit;

fastify.route({
url: '/',
method: 'GET',
bodyLimit: routeOptions?.['getApiInfo']?.bodyLimit ?? defaultBodyLimit,
schema: {
response: {
'200': {
Expand All @@ -101,12 +109,13 @@ export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceAp
} else {
throw new Error('Result must have exactly one set from: value, error');
}
}
},
});

fastify.route({
url: '/widgets',
method: 'GET',
bodyLimit: routeOptions?.['getWidgets']?.bodyLimit ?? defaultBodyLimit,
schema: {
response: {
'200': {
Expand Down Expand Up @@ -134,12 +143,13 @@ export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceAp
} else {
throw new Error('Result must have exactly one set from: value, error');
}
}
},
});

fastify.route({
url: '/widgets',
method: 'POST',
bodyLimit: routeOptions?.['createWidget']?.bodyLimit ?? defaultBodyLimit,
schema: {
response: {
'201': { $ref: 'Widget' },
Expand Down Expand Up @@ -168,12 +178,13 @@ export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceAp
} else {
throw new Error('Result must have exactly one set from: value, error');
}
}
},
});

fastify.route({
url: '/widgets/:id',
method: 'GET',
bodyLimit: routeOptions?.['getWidget']?.bodyLimit ?? defaultBodyLimit,
schema: {
response: {
'200': { $ref: 'Widget' },
Expand Down Expand Up @@ -208,12 +219,13 @@ export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceAp
} else {
throw new Error('Result must have exactly one set from: value, error');
}
}
},
});

fastify.route({
url: '/widgets/:id',
method: 'DELETE',
bodyLimit: routeOptions?.['deleteWidget']?.bodyLimit ?? defaultBodyLimit,
schema: {
response: {
'204': { type: 'object', additionalProperties: false },
Expand Down Expand Up @@ -247,12 +259,13 @@ export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceAp
} else {
throw new Error('Result must have exactly one set from: value, error');
}
}
},
});

fastify.route({
url: '/widgets/get',
method: 'POST',
bodyLimit: routeOptions?.['getWidgetBatch']?.bodyLimit ?? defaultBodyLimit,
schema: {
response: {
'200': { type: 'array', items: { type: 'object', properties: { value: { $ref: 'Widget' }, error: { $ref: '_error' } } } },
Expand All @@ -278,12 +291,13 @@ export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceAp
} else {
throw new Error('Result must have exactly one set from: value, error');
}
}
},
});

fastify.route({
url: '/mirrorFields',
method: 'POST',
bodyLimit: routeOptions?.['mirrorFields']?.bodyLimit ?? defaultBodyLimit,
schema: {
response: {
'200': {
Expand Down Expand Up @@ -313,12 +327,13 @@ export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceAp
} else {
throw new Error('Result must have exactly one set from: value, error');
}
}
},
});

fastify.route({
url: '/checkQuery',
method: 'GET',
bodyLimit: routeOptions?.['checkQuery']?.bodyLimit ?? defaultBodyLimit,
schema: {
response: {
'200': { type: 'object', additionalProperties: false },
Expand Down Expand Up @@ -349,12 +364,13 @@ export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceAp
} else {
throw new Error('Result must have exactly one set from: value, error');
}
}
},
});

fastify.route({
url: '/checkPath/:string/:boolean/:float/:double/:int32/:int64/:decimal/:enum/:datetime',
method: 'GET',
bodyLimit: routeOptions?.['checkPath']?.bodyLimit ?? defaultBodyLimit,
schema: {
response: {
'200': { type: 'object', additionalProperties: false },
Expand Down Expand Up @@ -385,12 +401,13 @@ export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceAp
} else {
throw new Error('Result must have exactly one set from: value, error');
}
}
},
});

fastify.route({
url: '/mirrorHeaders',
method: 'GET',
bodyLimit: routeOptions?.['mirrorHeaders']?.bodyLimit ?? defaultBodyLimit,
schema: {
response: {
'200': { type: 'object', additionalProperties: false },
Expand Down Expand Up @@ -431,12 +448,13 @@ export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceAp
} else {
throw new Error('Result must have exactly one set from: value, error');
}
}
},
});

fastify.route({
url: '/mixed/:path',
method: 'POST',
bodyLimit: routeOptions?.['mixed']?.bodyLimit ?? defaultBodyLimit,
schema: {
response: {
'200': {
Expand Down Expand Up @@ -483,12 +501,13 @@ export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceAp
} else {
throw new Error('Result must have exactly one set from: value, error');
}
}
},
});

fastify.route({
url: '/required',
method: 'POST',
bodyLimit: routeOptions?.['required']?.bodyLimit ?? defaultBodyLimit,
schema: {
response: {
'200': {
Expand Down Expand Up @@ -527,12 +546,13 @@ export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceAp
} else {
throw new Error('Result must have exactly one set from: value, error');
}
}
},
});

fastify.route({
url: '/mirrorBytes',
method: 'POST',
bodyLimit: routeOptions?.['mirrorBytes']?.bodyLimit ?? defaultBodyLimit,
schema: {
response: {
'200': { type: 'string' },
Expand Down Expand Up @@ -563,12 +583,13 @@ export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceAp
} else {
throw new Error('Result must have exactly one set from: value, error');
}
}
},
});

fastify.route({
url: '/mirrorText',
method: 'POST',
bodyLimit: routeOptions?.['mirrorText']?.bodyLimit ?? defaultBodyLimit,
schema: {
response: {
'200': { type: 'string' },
Expand Down Expand Up @@ -599,12 +620,13 @@ export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceAp
} else {
throw new Error('Result must have exactly one set from: value, error');
}
}
},
});

fastify.route({
url: '/bodyTypes',
method: 'POST',
bodyLimit: routeOptions?.['bodyTypes']?.bodyLimit ?? defaultBodyLimit,
schema: {
response: {
'200': { type: 'string' },
Expand All @@ -630,7 +652,7 @@ export const conformanceApiPlugin: fastifyTypes.FastifyPluginAsync<ConformanceAp
} else {
throw new Error('Result must have exactly one set from: value, error');
}
}
},
});
}

Expand Down
Loading
Loading