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
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ dotnet_diagnostic.IDE0161.severity = suggestion
dotnet_diagnostic.IDE0170.severity = suggestion
dotnet_diagnostic.IDE0180.severity = warning
dotnet_diagnostic.IDE1005.severity = suggestion
dotnet_diagnostic.JSON002.severity = suggestion
dotnet_diagnostic.SA0001.severity = none
dotnet_diagnostic.SA1003.severity = none
dotnet_diagnostic.SA1008.severity = none
Expand Down
253 changes: 253 additions & 0 deletions conformance/src/fastify/conformanceApiPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export type ConformanceApiPluginOptions = RegisterOptions & {
export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOptions> = async (fastify, opts) => {
const { api, caseInsenstiveQueryStringKeys, includeErrorDetails } = opts;

for (const jsonSchema of jsonSchemas) {
fastify.addSchema(jsonSchema);
}

fastify.setErrorHandler((error, req, res) => {
req.log.error(error);
if (includeErrorDetails) {
Expand Down Expand Up @@ -76,6 +80,17 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
fastify.route({
url: '/',
method: 'GET',
schema: {
response: {
200: {
type: 'object',
properties: {
service: { type: 'string' },
version: { type: 'string' },
},
},
},
},
handler: async function (req, res) {
const request: IGetApiInfoRequest = {};

Expand All @@ -99,6 +114,16 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
fastify.route({
url: '/widgets',
method: 'GET',
schema: {
response: {
200: {
type: 'object',
properties: {
widgets: { type: 'array', items: { $ref: 'Widget' } },
},
},
},
},
handler: async function (req, res) {
const request: IGetWidgetsRequest = {};

Expand All @@ -125,6 +150,11 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
fastify.route({
url: '/widgets',
method: 'POST',
schema: {
response: {
201: { $ref: 'Widget' },
},
},
handler: async function (req, res) {
const request: ICreateWidgetRequest = {};

Expand Down Expand Up @@ -155,6 +185,12 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
fastify.route({
url: '/widgets/:id',
method: 'GET',
schema: {
response: {
200: { $ref: 'Widget' },
304: { type: 'boolean' },
},
},
handler: async function (req, res) {
const request: IGetWidgetRequest = {};

Expand Down Expand Up @@ -193,6 +229,13 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
fastify.route({
url: '/widgets/:id',
method: 'DELETE',
schema: {
response: {
204: { type: 'object', additionalProperties: false },
404: { type: 'boolean' },
409: { type: 'boolean' },
},
},
handler: async function (req, res) {
const request: IDeleteWidgetRequest = {};

Expand Down Expand Up @@ -232,6 +275,11 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
fastify.route({
url: '/widgets/get',
method: 'POST',
schema: {
response: {
200: { type: 'array', items: { type: 'object', properties: { value: { $ref: 'Widget' }, error: { $ref: '_error' } } } },
},
},
handler: async function (req, res) {
const request: IGetWidgetBatchRequest = {};

Expand Down Expand Up @@ -259,6 +307,17 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
fastify.route({
url: '/mirrorFields',
method: 'POST',
schema: {
response: {
200: {
type: 'object',
properties: {
field: { $ref: 'Any' },
matrix: { type: 'array', items: { type: 'array', items: { type: 'array', items: { type: 'number' } } } },
},
},
},
},
handler: async function (req, res) {
const request: IMirrorFieldsRequest = {};

Expand Down Expand Up @@ -286,6 +345,11 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
fastify.route({
url: '/checkQuery',
method: 'GET',
schema: {
response: {
200: { type: 'object', additionalProperties: false },
},
},
handler: async function (req, res) {
const request: ICheckQueryRequest = {};

Expand Down Expand Up @@ -319,6 +383,11 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
fastify.route({
url: '/checkPath/:string/:boolean/:double/:int32/:int64/:decimal/:enum/:datetime',
method: 'GET',
schema: {
response: {
200: { type: 'object', additionalProperties: false },
},
},
handler: async function (req, res) {
const request: ICheckPathRequest = {};

Expand Down Expand Up @@ -352,6 +421,11 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
fastify.route({
url: '/mirrorHeaders',
method: 'GET',
schema: {
response: {
200: { type: 'object', additionalProperties: false },
},
},
handler: async function (req, res) {
const request: IMirrorHeadersRequest = {};

Expand Down Expand Up @@ -394,6 +468,18 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
fastify.route({
url: '/mixed/:path',
method: 'POST',
schema: {
response: {
200: {
type: 'object',
properties: {
normal: { type: 'string' },
},
},
202: { type: 'object', additionalProperties: true },
204: { type: 'boolean' },
},
},
handler: async function (req, res) {
const request: IMixedRequest = {};

Expand Down Expand Up @@ -441,6 +527,16 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
fastify.route({
url: '/required',
method: 'POST',
schema: {
response: {
200: {
type: 'object',
properties: {
normal: { type: 'string' },
},
},
},
},
handler: async function (req, res) {
const request: IRequiredRequest = {};

Expand Down Expand Up @@ -478,6 +574,11 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
fastify.route({
url: '/mirrorBytes',
method: 'POST',
schema: {
response: {
200: { type: 'string' },
},
},
handler: async function (req, res) {
const request: IMirrorBytesRequest = {};

Expand Down Expand Up @@ -510,6 +611,11 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
fastify.route({
url: '/mirrorText',
method: 'POST',
schema: {
response: {
200: { type: 'string' },
},
},
handler: async function (req, res) {
const request: IMirrorTextRequest = {};

Expand Down Expand Up @@ -542,6 +648,11 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
fastify.route({
url: '/bodyTypes',
method: 'POST',
schema: {
response: {
200: { type: 'string' },
},
},
handler: async function (req, res) {
const request: IBodyTypesRequest = {};

Expand All @@ -567,6 +678,148 @@ export const conformanceApiPlugin: FastifyPluginAsync<ConformanceApiPluginOption
});
}

const jsonSchemas = [
{
$id: '_error',
type: 'object',
properties: {
code: { type: 'string' },
message: { type: 'string' },
details: { type: 'object', additionalProperties: true },
innerError: { $ref: '_error' },
}
} as const,
{
$id: 'Widget',
type: 'object',
properties: {
id: { type: 'integer' },
name: { type: 'string' },
}
} as const,
{
$id: 'Any',
type: 'object',
properties: {
string: { type: 'string' },
boolean: { type: 'boolean' },
double: { type: 'number' },
int32: { type: 'integer' },
int64: { type: 'integer' },
decimal: { type: 'number' },
datetime: { type: 'string' },
bytes: { type: 'string' },
object: { type: 'object', additionalProperties: true },
error: { $ref: '_error' },
data: { $ref: 'Any' },
enum: { $ref: 'Answer' },
array: { $ref: 'AnyArray' },
map: { $ref: 'AnyMap' },
result: { $ref: 'AnyResult' },
nullable: { $ref: 'AnyNullable' },
}
} as const,
{
$id: 'AnyArray',
type: 'object',
properties: {
string: { type: 'array', items: { type: 'string' } },
boolean: { type: 'array', items: { type: 'boolean' } },
double: { type: 'array', items: { type: 'number' } },
int32: { type: 'array', items: { type: 'integer' } },
int64: { type: 'array', items: { type: 'integer' } },
decimal: { type: 'array', items: { type: 'number' } },
datetime: { type: 'array', items: { type: 'string' } },
bytes: { type: 'array', items: { type: 'string' } },
object: { type: 'array', items: { type: 'object', additionalProperties: true } },
error: { type: 'array', items: { $ref: '_error' } },
data: { type: 'array', items: { $ref: 'Any' } },
enum: { type: 'array', items: { $ref: 'Answer' } },
array: { type: 'array', items: { type: 'array', items: { type: 'integer' } } },
map: { type: 'array', items: { type: 'object', additionalProperties: { type: 'integer' } } },
result: { type: 'array', items: { type: 'object', properties: { value: { type: 'integer' }, error: { $ref: '_error' } } } },
nullable: { type: 'array', items: { oneOf: [ { type: 'integer' }, { type: 'null' } ] } },
}
} as const,
{
$id: 'AnyMap',
type: 'object',
properties: {
string: { type: 'object', additionalProperties: { type: 'string' } },
boolean: { type: 'object', additionalProperties: { type: 'boolean' } },
double: { type: 'object', additionalProperties: { type: 'number' } },
int32: { type: 'object', additionalProperties: { type: 'integer' } },
int64: { type: 'object', additionalProperties: { type: 'integer' } },
decimal: { type: 'object', additionalProperties: { type: 'number' } },
datetime: { type: 'object', additionalProperties: { type: 'string' } },
bytes: { type: 'object', additionalProperties: { type: 'string' } },
object: { type: 'object', additionalProperties: { type: 'object', additionalProperties: true } },
error: { type: 'object', additionalProperties: { $ref: '_error' } },
data: { type: 'object', additionalProperties: { $ref: 'Any' } },
enum: { type: 'object', additionalProperties: { $ref: 'Answer' } },
array: { type: 'object', additionalProperties: { type: 'array', items: { type: 'integer' } } },
map: { type: 'object', additionalProperties: { type: 'object', additionalProperties: { type: 'integer' } } },
result: { type: 'object', additionalProperties: { type: 'object', properties: { value: { type: 'integer' }, error: { $ref: '_error' } } } },
nullable: { type: 'object', additionalProperties: { oneOf: [ { type: 'integer' }, { type: 'null' } ] } },
}
} as const,
{
$id: 'AnyResult',
type: 'object',
properties: {
string: { type: 'object', properties: { value: { type: 'string' }, error: { $ref: '_error' } } },
boolean: { type: 'object', properties: { value: { type: 'boolean' }, error: { $ref: '_error' } } },
double: { type: 'object', properties: { value: { type: 'number' }, error: { $ref: '_error' } } },
int32: { type: 'object', properties: { value: { type: 'integer' }, error: { $ref: '_error' } } },
int64: { type: 'object', properties: { value: { type: 'integer' }, error: { $ref: '_error' } } },
decimal: { type: 'object', properties: { value: { type: 'number' }, error: { $ref: '_error' } } },
datetime: { type: 'object', properties: { value: { type: 'string' }, error: { $ref: '_error' } } },
bytes: { type: 'object', properties: { value: { type: 'string' }, error: { $ref: '_error' } } },
object: { type: 'object', properties: { value: { type: 'object', additionalProperties: true }, error: { $ref: '_error' } } },
error: { type: 'object', properties: { value: { $ref: '_error' }, error: { $ref: '_error' } } },
data: { type: 'object', properties: { value: { $ref: 'Any' }, error: { $ref: '_error' } } },
enum: { type: 'object', properties: { value: { $ref: 'Answer' }, error: { $ref: '_error' } } },
array: { type: 'object', properties: { value: { type: 'array', items: { type: 'integer' } }, error: { $ref: '_error' } } },
map: { type: 'object', properties: { value: { type: 'object', additionalProperties: { type: 'integer' } }, error: { $ref: '_error' } } },
result: { type: 'object', properties: { value: { type: 'object', properties: { value: { type: 'integer' }, error: { $ref: '_error' } } }, error: { $ref: '_error' } } },
nullable: { type: 'object', properties: { value: { oneOf: [ { type: 'integer' }, { type: 'null' } ] }, error: { $ref: '_error' } } },
}
} as const,
{
$id: 'AnyNullable',
type: 'object',
properties: {
string: { oneOf: [ { type: 'string' }, { type: 'null' } ] },
boolean: { oneOf: [ { type: 'boolean' }, { type: 'null' } ] },
double: { oneOf: [ { type: 'number' }, { type: 'null' } ] },
int32: { oneOf: [ { type: 'integer' }, { type: 'null' } ] },
int64: { oneOf: [ { type: 'integer' }, { type: 'null' } ] },
decimal: { oneOf: [ { type: 'number' }, { type: 'null' } ] },
datetime: { oneOf: [ { type: 'string' }, { type: 'null' } ] },
bytes: { oneOf: [ { type: 'string' }, { type: 'null' } ] },
object: { oneOf: [ { type: 'object', additionalProperties: true }, { type: 'null' } ] },
error: { oneOf: [ { $ref: '_error' }, { type: 'null' } ] },
data: { oneOf: [ { $ref: 'Any' }, { type: 'null' } ] },
enum: { oneOf: [ { $ref: 'Answer' }, { type: 'null' } ] },
array: { oneOf: [ { type: 'array', items: { type: 'integer' } }, { type: 'null' } ] },
map: { oneOf: [ { type: 'object', additionalProperties: { type: 'integer' } }, { type: 'null' } ] },
result: { oneOf: [ { type: 'object', properties: { value: { type: 'integer' }, error: { $ref: '_error' } } }, { type: 'null' } ] },
}
} as const,
{
$id: 'HasWidget',
type: 'object',
properties: {
widget: { $ref: 'Widget' },
}
} as const,
{
$id: 'Answer',
type: 'string',
enum: [ 'yes', 'no', 'maybe' ],
} as const,
] as const;

/** API for a Facility test server. */
export interface IConformanceApi {
/** Gets API information. */
Expand Down
Loading