Skip to content

Commit

Permalink
added first transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
theorm committed Nov 6, 2024
1 parent 202b3ab commit 85be18f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 5 deletions.
36 changes: 36 additions & 0 deletions src/hooks/transformation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { HookContext, HookFunction } from '@feathersjs/feathers'
import { ImpressoApplication } from '../types'

export const transformResponse = <S, I, O>(
transformer: (item: I) => O,
condition?: (context: HookContext<ImpressoApplication>) => boolean
): HookFunction<ImpressoApplication, S> => {
return context => {
if (context.type != 'after') throw new Error('The redactResponseDataItem hook should be used as an after hook only')
if (condition != null && !condition(context)) return context

if (context.result != null) {
const ctx = context as any
ctx.result = transformer(context.result as I)
}
return context
}
}

export const transformResponseDataItem = <S, I, O>(
transformer: (item: I) => O,
condition?: (context: HookContext<ImpressoApplication>) => boolean,
dataItemsField?: string
): HookFunction<ImpressoApplication, S> => {
return context => {
if (context.type != 'after') throw new Error('The redactResponseDataItem hook should be used as an after hook only')
if (condition != null && !condition(context)) return context

if (context.result != null) {
const result = context.result as Record<string, any>
const field = dataItemsField ?? 'data'
result[field] = result[field].map(transformer)
}
return context
}
}
2 changes: 1 addition & 1 deletion src/models/generated/schemasPublic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface ContentItem {
/**
* The type of the content item.
*/
type: "aricle" | "advert" | "obituary";
type: "article" | "advert" | "obituary";
/**
* The title of the content item
*/
Expand Down
2 changes: 1 addition & 1 deletion src/schema/schemasPublic/ContentItem.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"type": {
"type": "string",
"description": "The type of the content item.",
"enum": ["aricle", "advert", "obituary"]
"enum": ["article", "advert", "obituary"]
},
"title": {
"type": "string",
Expand Down
2 changes: 1 addition & 1 deletion src/services/collectable-items/collectable-items.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import lodash from 'lodash'
import { Op } from 'sequelize'
import CollectableItemGroup from '../../models/collectable-items-groups.model'
import { STATUS_DELETED, STATUS_PUBLIC, STATUS_SHARED } from '../../models/collections.model'
import { CollectableItemsUpdatedResponse, UpdateCollectableItems } from '../../models/generated/schemas'
import { CollectableItemsUpdatedResponse, UpdateCollectableItems } from '../../models/generated/shared'
import User from '../../models/users.model'
import { ImpressoApplication } from '../../types'
import { measureTime } from '../../util/instruments'
Expand Down
3 changes: 2 additions & 1 deletion src/services/search-facets/search-facets.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import lodash from 'lodash'
import { CachedSolrClient } from '../../cachedSolr'
import { SolrMappings } from '../../data/constants'
import { FindResponse } from '../../models/common'
import type { Filter, SearchFacet } from '../../models/generated/schemas'
import type { SearchFacet } from '../../models/generated/schemas'
import type { Filter } from '../../models/generated/shared'
import SearchFacetModel from '../../models/search-facets.model'
import { ImpressoApplication } from '../../types'
import { areCacheableFacets, isCacheableQuery } from '../../util/cache'
Expand Down
5 changes: 4 additions & 1 deletion src/services/search/search.hooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { authenticateAround as authenticate } from '../../hooks/authenticate'
import { rateLimit } from '../../hooks/rateLimiter'
import { redactResponseDataItem, defaultCondition } from '../../hooks/redaction'
import { redactResponseDataItem, defaultCondition, inPublicApi } from '../../hooks/redaction'
import { transformResponseDataItem } from '../../hooks/transformation'
import { transformContentItem } from '../../transformers/contentItem'
import { loadYamlFile } from '../../util/yaml'

const { protect } = require('@feathersjs/authentication-local').hooks
Expand Down Expand Up @@ -101,6 +103,7 @@ module.exports = {
displayQueryParams(['queryComponents', 'filters']),
resolveQueryComponents(),
protect('content'),
transformResponseDataItem(transformContentItem, inPublicApi),
redactResponseDataItem(articleRedactionPolicy, defaultCondition),
],
get: [],
Expand Down
23 changes: 23 additions & 0 deletions src/transformers/contentItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ContentItem as ContentItemPrivate } from '../models/generated/schemas'
import { ContentItem as ContentItemPublic } from '../models/generated/schemasPublic'

const toType = (input: string): ContentItemPublic['type'] => {
switch (input) {
case 'ar':
return 'article'
case 'ad':
return 'advert'
case 'ob':
return 'obituary'
default:
return 'article'
}
}

export const transformContentItem = (input: ContentItemPrivate): ContentItemPublic => {
return {
uid: input.uid,
title: input.title,
type: toType(input.type),
}
}

0 comments on commit 85be18f

Please sign in to comment.