Skip to content

Commit

Permalink
fixed openapi validator
Browse files Browse the repository at this point in the history
  • Loading branch information
theorm committed Apr 25, 2024
1 parent c9263e5 commit 9057be3
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 23 deletions.
15 changes: 12 additions & 3 deletions src/middleware/openApiValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@ import type { ImpressoApplication } from '../types'
import type { Application } from '@feathersjs/express'
import * as OpenApiValidator from 'express-openapi-validator'
import fs from 'fs'
import { logger } from '../logger'

export default async (app: ImpressoApplication & Application) => {
export default (app: ImpressoApplication & Application) => {
init(app).catch(e => {
logger.error('Important: Failed to initialize OpenAPI validator middleware', e)
})
}

const init = async (app: ImpressoApplication & Application) => {
const isPublicApi = app.get('isPublicApi')
if (!isPublicApi) return

if (!('docs' in app)) throw new Error('`docs` property not found in app object. Is swagger initialized?')

const spec = (app as any)['docs'] as unknown as OpenAPIV3.Document
const specCopy = spec //JSON.parse(JSON.stringify(spec))

const dereferencedOpenApiSpec = await RefParser.dereference(spec, {
const dereferencedOpenApiSpec = await RefParser.bundle(specCopy, {
resolve: {
file: {
/**
Expand All @@ -27,12 +36,12 @@ export default async (app: ImpressoApplication & Application) => {
},
},
})

const middlewares = OpenApiValidator.middleware({
apiSpec: dereferencedOpenApiSpec as unknown as OpenAPIV3.Document,
validateRequests: true, // (default)
validateResponses: true, // false by default
validateApiSpec: false,
})
middlewares.forEach(middleware => app.use(middleware))
logger.info('OpenAPI validator middleware loaded')
}
158 changes: 149 additions & 9 deletions src/models/generated/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
*/
export interface SearchFindResponse {
data: any[];
/**
* Additional information about the response.
*/
info: { [key: string]: any };
/**
* The number of items returned in this response
*/
limit: number;
/**
* The number of items skipped in this response
*/
skip: number;
/**
* The total number of items matching the query
*/
total: number;
}

/**
Expand Down Expand Up @@ -140,9 +156,6 @@ export interface User {
username: string;
}

/**
* Search find response (articles)
*/
export interface BaseFindResponse {
data: any[];
/**
Expand All @@ -165,8 +178,6 @@ export interface BaseFindResponse {

/**
* Collectable Item find response
*
* Search find response (articles)
*/
export interface CollectableItemFindResponse {
data: any[];
Expand Down Expand Up @@ -195,8 +206,6 @@ export interface BaseUser {

/**
* Collections find response
*
* Search find response (articles)
*/
export interface CollectionsFindResponse {
data: any[];
Expand Down Expand Up @@ -245,6 +254,139 @@ export interface Task {
task_id?: string;
}

/**
* Response for the find newspapers endpoint.
*/
export interface FindNewspapersResponse {
data: any[];
}

/**
* A newspaper
*/
export interface NewspapersGet {
/**
* The acronym of the newspaper
*/
acronym: string;
/**
* The number of articles in the newspaper
*/
countArticles: number;
/**
* The number of issues in the newspaper
*/
countIssues: number;
/**
* The number of pages in the newspaper
*/
countPages: number;
/**
* The number of years of the newspaper available
*/
deltaYear: number;
/**
* Last available year of the newspaper articles
*/
endYear: string;
/**
* TODO
*/
fetched?: boolean;
/**
* First available issue of the newspaper
*/
firstIssue: NewspaperIssue;
/**
* TODO
*/
included: boolean;
/**
* The labels of the newspaper
*/
labels: string[];
/**
* Language codes of the languages used in the newspaper
*/
languages: string[];
/**
* Last available issue of the newspaper
*/
lastIssue: NewspaperIssue;
/**
* Title of the newspaper
*/
name: string;
/**
* TODO
*/
properties?: NewspaperProperty[];
/**
* First available year of the newspaper articles
*/
startYear: string;
/**
* The unique identifier of the newspaper
*/
uid: string;
}

/**
* First available issue of the newspaper
*
* Last available issue of the newspaper
*/
export interface NewspaperIssue {
/**
* TODO: list available options
*/
accessRights: string;
/**
* TODO
*/
cover: string;
/**
* The date of the issue
*/
date: Date;
/**
* TODO
*/
fresh: boolean;
/**
* The labels of the issue
*/
labels: string[];
/**
* The unique identifier of the issue
*/
uid: string;
/**
* The year of the issue
*/
year: string;
}

export interface NewspaperProperty {
/**
* Whether the value is a URL
*/
isUrl?: boolean;
/**
* The label of the property
*/
label: string;
/**
* The name of the property
*/
name: string;
/**
* The value of the property
*/
value: string;
[property: string]: any;
}

export interface FindTextReuseClustersResponse {
clusters: GetTextReuseClusterResponse[];
info: Pagination;
Expand Down Expand Up @@ -351,8 +493,6 @@ export interface Pagination {

/**
* Collections find response
*
* Search find response (articles)
*/
export interface TextReusePassageFindResponse {
data: any[];
Expand Down
130 changes: 128 additions & 2 deletions src/models/generated/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,132 @@ export enum Precision {
Soft = "soft",
}

/**
* A newspaper
*/
export interface Newspaper {
/**
* The acronym of the newspaper
*/
acronym: string;
/**
* The number of articles in the newspaper
*/
countArticles: number;
/**
* The number of issues in the newspaper
*/
countIssues: number;
/**
* The number of pages in the newspaper
*/
countPages: number;
/**
* The number of years of the newspaper available
*/
deltaYear: number;
/**
* Last available year of the newspaper articles
*/
endYear: string;
/**
* TODO
*/
fetched?: boolean;
/**
* First available issue of the newspaper
*/
firstIssue: NewspaperIssue;
/**
* TODO
*/
included: boolean;
/**
* The labels of the newspaper
*/
labels: string[];
/**
* Language codes of the languages used in the newspaper
*/
languages: string[];
/**
* Last available issue of the newspaper
*/
lastIssue: NewspaperIssue;
/**
* Title of the newspaper
*/
name: string;
/**
* TODO
*/
properties?: NewspaperProperty[];
/**
* First available year of the newspaper articles
*/
startYear: string;
/**
* The unique identifier of the newspaper
*/
uid: string;
}

/**
* First available issue of the newspaper
*
* Last available issue of the newspaper
*/
export interface NewspaperIssue {
/**
* TODO: list available options
*/
accessRights: string;
/**
* TODO
*/
cover: string;
/**
* The date of the issue
*/
date: Date;
/**
* TODO
*/
fresh: boolean;
/**
* The labels of the issue
*/
labels: string[];
/**
* The unique identifier of the issue
*/
uid: string;
/**
* The year of the issue
*/
year: string;
}

export interface NewspaperProperty {
/**
* Whether the value is a URL
*/
isUrl?: boolean;
/**
* The label of the property
*/
label: string;
/**
* The name of the property
*/
name: string;
/**
* The value of the property
*/
value: string;
[property: string]: any;
}

/**
* Represents a cluster of text reuse passages
*/
Expand Down Expand Up @@ -317,7 +443,7 @@ export interface TextReusePassage {
/**
* Newspaper details
*/
newspaper?: Newspaper;
newspaper?: NewspaperClass;
offsetEnd: number;
offsetStart: number;
/**
Expand Down Expand Up @@ -375,7 +501,7 @@ export interface Issue {
/**
* Newspaper details
*/
export interface Newspaper {
export interface NewspaperClass {
/**
* ID of the newspaper
*/
Expand Down
Loading

0 comments on commit 9057be3

Please sign in to comment.