|
| 1 | +import { Router, Request } from 'express'; |
| 2 | + |
| 3 | +import { |
| 4 | + ElementsToHideOrRemoveEntry, |
| 5 | + elementsToHideOrRemoveMethods, |
| 6 | + filterRules |
| 7 | +} from '../../advertising/external-ads'; |
| 8 | +import { addObjectStorageMethodsToRouter } from '../../utils/express-helpers'; |
| 9 | +import { transformValues } from '../../utils/helpers'; |
| 10 | +import { elementsToHideOrRemoveDictionarySchema, hostnamesListSchema } from '../../utils/schemas'; |
| 11 | + |
| 12 | +export const elementsToHideOrRemoveRouter = Router(); |
| 13 | + |
| 14 | +const transformElementsToHideOrRemoveRules = (value: ElementsToHideOrRemoveEntry[], req: Request) => |
| 15 | + filterRules(value, req.query.extVersion as string | undefined); |
| 16 | +const transformRulesDictionary = (value: Record<string, ElementsToHideOrRemoveEntry[]>, req: Request) => |
| 17 | + transformValues(value, rules => filterRules(rules, req.query.extVersion as string | undefined)); |
| 18 | + |
| 19 | +/** |
| 20 | + * @swagger |
| 21 | + * tags: |
| 22 | + * name: Elements to hide or remove |
| 23 | + * components: |
| 24 | + * schemas: |
| 25 | + * ElementsToHideOrRemoveEntry: |
| 26 | + * allOf: |
| 27 | + * - $ref: '#/components/schemas/ExtVersionConstraints' |
| 28 | + * - type: object |
| 29 | + * required: |
| 30 | + * - cssString |
| 31 | + * - parentDepth |
| 32 | + * - isMultiple |
| 33 | + * - urlRegexes |
| 34 | + * - shouldHide |
| 35 | + * properties: |
| 36 | + * cssString: |
| 37 | + * type: string |
| 38 | + * parentDepth: |
| 39 | + * type: number |
| 40 | + * min: 0 |
| 41 | + * integer: true |
| 42 | + * description: > |
| 43 | + * Indicates the depth of the parent element of the selected element |
| 44 | + * isMultiple: |
| 45 | + * type: boolean |
| 46 | + * description: Whether the selector should select multiple elements |
| 47 | + * urlRegexes: |
| 48 | + * type: array |
| 49 | + * items: |
| 50 | + * type: string |
| 51 | + * format: regex |
| 52 | + * shouldHide: |
| 53 | + * type: boolean |
| 54 | + * ElementsToHideOrRemoveDictionary: |
| 55 | + * type: object |
| 56 | + * additionalProperties: |
| 57 | + * type: array |
| 58 | + * items: |
| 59 | + * $ref: '#/components/schemas/ElementsToHideOrRemoveEntry' |
| 60 | + * example: |
| 61 | + * 'm.economictimes.com': |
| 62 | + * - extVersion: '>=1.21.1' |
| 63 | + * cssString: '#iframeDisplay, #closeDisplay' |
| 64 | + * parentDepth: 0 |
| 65 | + * isMultiple: true |
| 66 | + * urlRegexes: |
| 67 | + * - "^https://m\\.economictimes\\.com" |
| 68 | + * shouldHide: true |
| 69 | + * /api/slise-ad-rules/elements-to-hide-or-remove/raw/all: |
| 70 | + * get: |
| 71 | + * summary: Get all rules for hiding or removing elements |
| 72 | + * tags: |
| 73 | + * - Elements to hide or remove |
| 74 | + * responses: |
| 75 | + * '200': |
| 76 | + * description: A dictionary of all rules |
| 77 | + * content: |
| 78 | + * application/json: |
| 79 | + * schema: |
| 80 | + * $ref: '#/components/schemas/ElementsToHideOrRemoveDictionary' |
| 81 | + * '500': |
| 82 | + * $ref: '#/components/responses/ErrorResponse' |
| 83 | + * /api/slise-ad-rules/elements-to-hide-or-remove/{domain}/raw: |
| 84 | + * get: |
| 85 | + * summary: Get rules for hiding or removing elements by domain |
| 86 | + * tags: |
| 87 | + * - Elements to hide or remove |
| 88 | + * parameters: |
| 89 | + * - name: domain |
| 90 | + * in: path |
| 91 | + * required: true |
| 92 | + * schema: |
| 93 | + * type: string |
| 94 | + * description: Domain name |
| 95 | + * responses: |
| 96 | + * '200': |
| 97 | + * description: An array of rules |
| 98 | + * content: |
| 99 | + * application/json: |
| 100 | + * schema: |
| 101 | + * type: array |
| 102 | + * items: |
| 103 | + * $ref: '#/components/schemas/ElementsToHideOrRemoveEntry' |
| 104 | + * '500': |
| 105 | + * $ref: '#/components/responses/ErrorResponse' |
| 106 | + * /api/slise-ad-rules/elements-to-hide-or-remove/{domain}: |
| 107 | + * get: |
| 108 | + * summary: Get rules for hiding or removing elements by domain and extension version |
| 109 | + * tags: |
| 110 | + * - Elements to hide or remove |
| 111 | + * parameters: |
| 112 | + * - name: domain |
| 113 | + * in: path |
| 114 | + * required: true |
| 115 | + * schema: |
| 116 | + * type: string |
| 117 | + * description: Domain name |
| 118 | + * - name: extVersion |
| 119 | + * in: query |
| 120 | + * schema: |
| 121 | + * type: string |
| 122 | + * default: '0.0.0' |
| 123 | + * description: Extension version |
| 124 | + * responses: |
| 125 | + * '200': |
| 126 | + * description: An array of rules |
| 127 | + * content: |
| 128 | + * application/json: |
| 129 | + * schema: |
| 130 | + * type: array |
| 131 | + * items: |
| 132 | + * $ref: '#/components/schemas/ElementsToHideOrRemoveEntry' |
| 133 | + * '500': |
| 134 | + * $ref: '#/components/responses/ErrorResponse' |
| 135 | + * /api/slise-ad-rules/elements-to-hide-or-remove: |
| 136 | + * get: |
| 137 | + * summary: Get all rules for hiding or removing elements filtered by extension version |
| 138 | + * tags: |
| 139 | + * - Elements to hide or remove |
| 140 | + * parameters: |
| 141 | + * - name: extVersion |
| 142 | + * in: query |
| 143 | + * schema: |
| 144 | + * type: string |
| 145 | + * default: '0.0.0' |
| 146 | + * description: Extension version |
| 147 | + * responses: |
| 148 | + * '200': |
| 149 | + * description: A dictionary of rules |
| 150 | + * content: |
| 151 | + * application/json: |
| 152 | + * schema: |
| 153 | + * $ref: '#/components/schemas/ElementsToHideOrRemoveDictionary' |
| 154 | + * '500': |
| 155 | + * $ref: '#/components/responses/ErrorResponse' |
| 156 | + * post: |
| 157 | + * summary: Add rules for hiding or removing elements. If a rule already exists, it will be updated |
| 158 | + * tags: |
| 159 | + * - Elements to hide or remove |
| 160 | + * security: |
| 161 | + * - basicAuth: [] |
| 162 | + * requestBody: |
| 163 | + * content: |
| 164 | + * application/json: |
| 165 | + * schema: |
| 166 | + * $ref: '#/components/schemas/ElementsToHideOrRemoveDictionary' |
| 167 | + * responses: |
| 168 | + * '200': |
| 169 | + * $ref: '#/components/responses/SuccessResponse' |
| 170 | + * '400': |
| 171 | + * $ref: '#/components/responses/ErrorResponse' |
| 172 | + * '401': |
| 173 | + * $ref: '#/components/responses/UnauthorizedError' |
| 174 | + * '500': |
| 175 | + * $ref: '#/components/responses/ErrorResponse' |
| 176 | + * delete: |
| 177 | + * summary: Delete rules for hiding or removing elements |
| 178 | + * tags: |
| 179 | + * - Elements to hide or remove |
| 180 | + * security: |
| 181 | + * - basicAuth: [] |
| 182 | + * requestBody: |
| 183 | + * content: |
| 184 | + * application/json: |
| 185 | + * schema: |
| 186 | + * type: array |
| 187 | + * items: |
| 188 | + * type: string |
| 189 | + * example: |
| 190 | + * - 'm.economictimes.com' |
| 191 | + * responses: |
| 192 | + * '200': |
| 193 | + * $ref: '#/components/responses/SuccessResponse' |
| 194 | + * '400': |
| 195 | + * $ref: '#/components/responses/ErrorResponse' |
| 196 | + * '401': |
| 197 | + * $ref: '#/components/responses/UnauthorizedError' |
| 198 | + * '500': |
| 199 | + * $ref: '#/components/responses/ErrorResponse' |
| 200 | + */ |
| 201 | +addObjectStorageMethodsToRouter(elementsToHideOrRemoveRouter, { |
| 202 | + path: '/', |
| 203 | + methods: elementsToHideOrRemoveMethods, |
| 204 | + keyName: 'domain', |
| 205 | + objectValidationSchema: elementsToHideOrRemoveDictionarySchema, |
| 206 | + keysArrayValidationSchema: hostnamesListSchema, |
| 207 | + successfulRemovalMessage: entriesCount => `${entriesCount} entries have been removed`, |
| 208 | + objectTransformFn: transformRulesDictionary, |
| 209 | + valueTransformFn: transformElementsToHideOrRemoveRules |
| 210 | +}); |
0 commit comments