Skip to content

Commit

Permalink
refactor public base find response
Browse files Browse the repository at this point in the history
  • Loading branch information
theorm committed Nov 13, 2024
1 parent 5b9effd commit 1bc3365
Show file tree
Hide file tree
Showing 18 changed files with 116 additions and 16 deletions.
19 changes: 19 additions & 0 deletions src/models/generated/schemasPublic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@
*/


export interface BaseFindResponse {
data: unknown[];
pagination: {
/**
* The total number of items matching the query
*/
total: number;
/**
* The number of items returned in this response
*/
limit: number;
/**
* Starting index of the items subset returned in this response
*/
offset: number;
};
}


/**
* Collection details.
*/
Expand Down
32 changes: 32 additions & 0 deletions src/schema/schemasPublic/BaseFind.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "Base Find Response",
"properties": {
"data": {
"type": "array",
"items": {}
},
"pagination": {
"type": "object",
"additionalProperties": false,
"properties": {
"total": {
"type": "integer",
"description": "The total number of items matching the query"
},
"limit": {
"type": "integer",
"description": "The number of items returned in this response"
},
"offset": {
"type": "integer",
"description": "Starting index of the items subset returned in this response"
}
},
"required": ["total", "limit", "offset"]
}
},
"required": ["data", "pagination"],
"additionalProperties": false
}
6 changes: 5 additions & 1 deletion src/services/collections/collections.hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { rateLimit } from '../../hooks/rateLimiter'
import { transformResponseDataItem, transformResponse, renameQueryParameters } from '../../hooks/transformation'
import { inPublicApi } from '../../hooks/redaction'
import { transformCollection } from '../../transformers/collection'
import { transformBaseFind } from '../../transformers/base'

const { queryWithCommonParams, validate, utils, REGEX_UIDS } = require('../../hooks/params')

Expand Down Expand Up @@ -94,7 +95,10 @@ module.exports = {

after: {
all: [],
find: [transformResponseDataItem(transformCollection, inPublicApi)],
find: [
transformResponse(transformBaseFind, inPublicApi),
transformResponseDataItem(transformCollection, inPublicApi),
],
get: [transformResponse(transformCollection, inPublicApi)],
create: [transformResponse(transformCollection, inPublicApi)],
update: [],
Expand Down
5 changes: 5 additions & 0 deletions src/services/collections/collections.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const getDocs = (isPublicApi: boolean): ServiceSwaggerOptions => ({
responses: getStandardResponses({
method: 'find',
schema: 'Collection',
isPublic: isPublicApi,
}),
},
get: {
Expand All @@ -91,6 +92,7 @@ export const getDocs = (isPublicApi: boolean): ServiceSwaggerOptions => ({
responses: getStandardResponses({
method: 'get',
schema: 'Collection',
isPublic: isPublicApi,
}),
},
create: {
Expand All @@ -102,6 +104,7 @@ export const getDocs = (isPublicApi: boolean): ServiceSwaggerOptions => ({
responses: getStandardResponses({
method: 'create',
schema: 'Collection',
isPublic: isPublicApi,
}),
},
patch: {
Expand All @@ -124,6 +127,7 @@ export const getDocs = (isPublicApi: boolean): ServiceSwaggerOptions => ({
responses: getStandardResponses({
method: 'patch',
schema: 'Collection',
isPublic: isPublicApi,
}),
},
remove: {
Expand All @@ -143,6 +147,7 @@ export const getDocs = (isPublicApi: boolean): ServiceSwaggerOptions => ({
responses: getStandardResponses({
method: 'remove',
schema: 'CollectionsRemoveResponse',
isPublic: isPublicApi,
}),
},
},
Expand Down
6 changes: 5 additions & 1 deletion src/services/entities/entities.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { qToSolrFilter, filtersToSolrQuery } = require('../../hooks/search')
import { transformResponseDataItem, transformResponse, renameQueryParameters } from '../../hooks/transformation'
import { inPublicApi } from '../../hooks/redaction'
import { transformEntityDetails } from '../../transformers/entity'
import { transformBaseFind } from '../../transformers/base'

const orderByMap = {
relevance: 'score ASC',
Expand Down Expand Up @@ -96,7 +97,10 @@ export default {

after: {
all: [],
find: [transformResponseDataItem(transformEntityDetails, inPublicApi)],
find: [
transformResponse(transformBaseFind, inPublicApi),
transformResponseDataItem(transformEntityDetails, inPublicApi),
],
get: [transformResponse(transformEntityDetails, inPublicApi)],
create: [],
update: [],
Expand Down
2 changes: 2 additions & 0 deletions src/services/entities/entities.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const getDocs = (isPublicApi: boolean): ServiceSwaggerOptions => ({
responses: getStandardResponses({
method: 'find',
schema: 'EntityDetails',
isPublic: isPublicApi,
}),
},
get: {
Expand All @@ -93,6 +94,7 @@ export const getDocs = (isPublicApi: boolean): ServiceSwaggerOptions => ({
responses: getStandardResponses({
method: 'get',
schema: 'EntityDetails',
isPublic: isPublicApi,
}),
},
},
Expand Down
6 changes: 5 additions & 1 deletion src/services/newspapers/newspapers.hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { OrderByChoices } from './newspapers.schema'
import { transformResponseDataItem, transformResponse, renameQueryParameters } from '../../hooks/transformation'
import { inPublicApi } from '../../hooks/redaction'
import { transformNewspaper } from '../../transformers/newspaper'
import { transformBaseFind } from '../../transformers/base'

const { queryWithCommonParams, validate, utils } = require('../../hooks/params')
const { checkCachedContents, returnCachedContents, saveResultsInCache } = require('../../hooks/redis')
Expand Down Expand Up @@ -74,7 +75,10 @@ module.exports = {

after: {
all: [returnCachedContents(), saveResultsInCache()],
find: [transformResponseDataItem(transformNewspaper, inPublicApi)],
find: [
transformResponse(transformBaseFind, inPublicApi),
transformResponseDataItem(transformNewspaper, inPublicApi),
],
get: [transformResponse(transformNewspaper, inPublicApi)],
create: [],
update: [],
Expand Down
2 changes: 2 additions & 0 deletions src/services/newspapers/newspapers.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export const getDocs = (isPublicApi: boolean): ServiceSwaggerOptions => ({
responses: getStandardResponses({
method: 'find',
schema: 'Newspaper',
isPublic: isPublicApi,
}),
},
get: {
Expand All @@ -114,6 +115,7 @@ export const getDocs = (isPublicApi: boolean): ServiceSwaggerOptions => ({
responses: getStandardResponses({
method: 'get',
schema: 'Newspaper',
isPublic: isPublicApi,
}),
},
},
Expand Down
1 change: 1 addition & 0 deletions src/services/search-facets/search-facets.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export const getDocs = (index: IndexId, isPublicApi: boolean): ServiceSwaggerOpt
responses: getStandardResponses({
method: 'find',
schema: 'SearchFacetBucket',
isPublic: isPublicApi,
}),
},
},
Expand Down
4 changes: 3 additions & 1 deletion src/services/search/search.hooks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { authenticateAround as authenticate } from '../../hooks/authenticate'
import { rateLimit } from '../../hooks/rateLimiter'
import { redactResponseDataItem, defaultCondition, inPublicApi } from '../../hooks/redaction'
import { transformResponseDataItem, renameQueryParameters } from '../../hooks/transformation'
import { transformResponseDataItem, transformResponse, renameQueryParameters } from '../../hooks/transformation'
import { transformBaseFind } from '../../transformers/base'
import { transformContentItem } from '../../transformers/contentItem'
import { loadYamlFile } from '../../util/yaml'

Expand Down Expand Up @@ -106,6 +107,7 @@ module.exports = {
all: [],
find: [
displayQueryParams(['queryComponents', 'filters']),
transformResponse(transformBaseFind, inPublicApi),
resolveQueryComponents(),
protect('content'),
transformResponseDataItem(transformContentItem, inPublicApi),
Expand Down
1 change: 1 addition & 0 deletions src/services/search/search.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const getDocs = (isPublicApi: boolean): ServiceSwaggerOptions => ({
responses: getStandardResponses({
method: 'find',
schema: 'ContentItem',
isPublic: isPublicApi,
}),
},
},
Expand Down
2 changes: 2 additions & 0 deletions src/services/text-reuse-clusters/text-reuse-clusters.hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
renameQueryParameters,
} from '../../hooks/transformation'
import { transformTextReuseCluster } from '../../transformers/textReuse'
import { transformBaseFind } from '../../transformers/base'

// const { validateWithSchema } = require('../../hooks/schema')

Expand Down Expand Up @@ -52,6 +53,7 @@ module.exports = {
],
find: [
renameTopLevelField(['clusters', 'data'], inPublicApi),
transformResponse(transformBaseFind, inPublicApi),
transformResponseDataItem(transformTextReuseCluster, inPublicApi),
redactResponseDataItem(trPassageRedactionPolicy, defaultCondition),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const getDocs = (isPublicApi: boolean): ServiceSwaggerOptions => ({
responses: getStandardResponses({
method: 'find',
schema: 'TextReuseCluster',
isPublic: isPublicApi,
}),
},
get: {
Expand All @@ -98,6 +99,7 @@ export const getDocs = (isPublicApi: boolean): ServiceSwaggerOptions => ({
responses: getStandardResponses({
method: 'get',
schema: 'TextReuseCluster',
isPublic: isPublicApi,
}),
},
},
Expand Down
2 changes: 2 additions & 0 deletions src/services/text-reuse-passages/text-reuse-passages.hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { redactResponse, redactResponseDataItem, defaultCondition, inPublicApi }
import { loadYamlFile } from '../../util/yaml'
import { transformResponseDataItem, transformResponse } from '../../hooks/transformation'
import { transformTextReusePassage } from '../../transformers/textReuse'
import { transformBaseFind } from '../../transformers/base'

const trPassageRedactionPolicy = loadYamlFile(`${__dirname}/resources/trPassageRedactionPolicy.yml`)

Expand Down Expand Up @@ -39,6 +40,7 @@ module.exports = {
redactResponse(trPassageRedactionPolicy, defaultCondition),
],
find: [
transformResponse(transformBaseFind, inPublicApi),
transformResponseDataItem(transformTextReusePassage, inPublicApi),
redactResponseDataItem(trPassageRedactionPolicy, defaultCondition),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const getDocs = (isPublicApi: boolean): ServiceSwaggerOptions => ({
responses: getStandardResponses({
method: 'find',
schema: 'TextReusePassage',
isPublic: isPublicApi,
}),
},
get: {
Expand All @@ -75,6 +76,7 @@ export const getDocs = (isPublicApi: boolean): ServiceSwaggerOptions => ({
responses: getStandardResponses({
method: 'get',
schema: 'TextReusePassage',
isPublic: isPublicApi,
}),
},
},
Expand Down
13 changes: 13 additions & 0 deletions src/transformers/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { BaseFind } from '../models/generated/schemas'
import { BaseFindResponse as BaseFindPublic } from '../models/generated/schemasPublic'

export const transformBaseFind = (input: BaseFind): BaseFindPublic => {
return {
pagination: {
total: input.total,
limit: input.limit,
offset: input.offset,
},
data: input.data,
}
}
16 changes: 7 additions & 9 deletions src/transformers/searchFacet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import { ImpressoApplication } from '../types'

import {
SearchFacet,
BaseFind,
SearchFacetBucket as SearchFacetBucketInternal,
SearchFacetRangeBucket,
} from '../models/generated/schemas'
import { SearchFacetBucket } from '../models/generated/schemasPublic'
import { SearchFacetBucket, BaseFindResponse } from '../models/generated/schemasPublic'
import Collection from '../models/collections.model'
import Newspaper from '../models/newspapers.model'
import Entity from '../models/entities.model'
import Topic from '../models/topics.model'

interface FacetContainer extends BaseFind {
interface FacetContainer extends BaseFindResponse {
data: SearchFacetBucket[]
}

Expand Down Expand Up @@ -77,13 +76,12 @@ const transformBucket = (
}

export const transformSearchFacet = (input: SearchFacet, context: HookContext<ImpressoApplication>): FacetContainer => {
console.log('III', input, context.id)

return {
pagination: {
total: input.numBuckets,
limit: context.params?.query?.limit ?? input.buckets.length,
offset: context.params?.query?.offset ?? 0,
},
data: input.buckets.map(b => transformBucket(b, context.id as string)),
total: input.numBuckets,
limit: context.params?.query?.limit ?? input.buckets.length,
offset: context.params?.query?.offset ?? 0,
info: {},
}
}
11 changes: 8 additions & 3 deletions src/util/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ interface GetStandardResponsesParams {
authEnabled?: boolean
isRateLimited?: boolean
standardPagination?: boolean
isPublic?: boolean
}

const baseFindResponse = require('../schema/schemas/BaseFind.json')
delete baseFindResponse['$schema']

const getBaseFindResponse = (itemRef: string): JSONSchema => {
const response = JSON.parse(JSON.stringify(baseFindResponse))
const baseFindResponsePublic = require('../schema/schemasPublic/BaseFind.json')
delete baseFindResponsePublic['$schema']

const getBaseFindResponse = (itemRef: string, isPublic: boolean): JSONSchema => {
const response = JSON.parse(JSON.stringify(isPublic ? baseFindResponsePublic : baseFindResponse))
response['properties']['data']['items'] = {
$ref: itemRef,
}
Expand All @@ -84,6 +88,7 @@ export const getStandardResponses = ({
authEnabled = true,
isRateLimited = true,
standardPagination = true,
isPublic = false,
}: GetStandardResponsesParams) => {
const defaultResponses: Record<number, StatusResponse> = {
422: {
Expand All @@ -107,7 +112,7 @@ export const getStandardResponses = ({
if (method === 'find' && standardPagination) {
defaultResponses[200] = {
description: 'Success',
content: asApplicationJson(getBaseFindResponse(`#/components/schemas/${schema}`)),
content: asApplicationJson(getBaseFindResponse(`#/components/schemas/${schema}`, isPublic)),
headers: { ...defaultHeaders },
}
} else {
Expand Down

0 comments on commit 1bc3365

Please sign in to comment.