-
Notifications
You must be signed in to change notification settings - Fork 0
feat(GlobalRecs): Add /v3/firefox/global-recs API #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
785df46
a9e9a6e
1c70bf7
110b60b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import express, { NextFunction, Request, Response } from 'express'; | ||
| import config from '../../config'; | ||
| import CacheControlHandler from '../lib/cacheControlHandler'; | ||
| import ConsumerKeyHandler from '../../auth/consumerKeyHandler'; | ||
| import { GraphQLErrorHandler } from '../error/graphQLErrorHandler'; | ||
| import { handleQueryParameters } from './inputs'; | ||
| import { BFFFxError } from '../../bfffxError'; | ||
| import Recommendations from '../../graphql-proxy/recommendations/recommendations'; | ||
| import { forwardHeadersMiddleware } from '../../graphql-proxy/lib/client'; | ||
| import { RecommendationsQueryVariables } from '../../generated/graphql/types'; | ||
| import { GlobalRecsResponse, responseTransformer } from './response'; | ||
|
|
||
| const router = express.Router(); | ||
|
|
||
| router.get( | ||
| '/firefox/global-recs', | ||
| // request must include a consumer | ||
| ConsumerKeyHandler, | ||
| CacheControlHandler('public, max-age=1800', config), | ||
| async (req: Request, res: Response, next: NextFunction) => { | ||
| try { | ||
| const variables = handleQueryParameters(req.query); | ||
|
|
||
| if (variables instanceof BFFFxError) { | ||
| return next(variables); | ||
| } | ||
|
|
||
| const graphRes = await Recommendations({ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For future enhancement it: Given all requests in this repo is probably forwarding a request to the graph it would be great to have either a middleware or some factory to generate a graph client for us with all of the authentication parameters already handled. |
||
| auth: req.auth, | ||
| consumer_key: req.consumer_key, | ||
| forwardHeadersMiddleware: forwardHeadersMiddleware(res), | ||
| variables: variables as RecommendationsQueryVariables, | ||
| }); | ||
|
|
||
| res.json(responseTransformer(graphRes) as GlobalRecsResponse); | ||
| } catch (error) { | ||
| const responseError = GraphQLErrorHandler(error); | ||
| return next(responseError); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| export default router; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { faker } from '@faker-js/faker'; | ||
| import assert from 'assert'; | ||
|
|
||
| import { RecommendationsQueryVariables } from '../../generated/graphql/types'; | ||
| import { | ||
| GlobalRecsQueryParameterStrings, | ||
| handleQueryParameters, | ||
| setDefaultsAndCoerceTypes, | ||
| } from './inputs'; | ||
|
|
||
| import { APIError, APIErrorResponse, BFFFxError } from '../../bfffxError'; | ||
|
|
||
| describe('input.ts recommendations query parameters', () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great example of tests and covering all the conditionals in your unit tests. |
||
| describe('setDefaultsAndCoerceTypes', () => { | ||
| it('converts count to an integer and passes through others', () => { | ||
| const res = setDefaultsAndCoerceTypes({ | ||
| count: '3', | ||
| locale_lang: 'preValidatedLocale', | ||
| region: 'preValidatedRegion', | ||
| }); | ||
| expect(res).toMatchObject({ | ||
| count: 3, | ||
| locale: 'preValidatedLocale', | ||
| region: 'preValidatedRegion', | ||
| }); | ||
| }); | ||
|
|
||
| it('sets count to 20 if no default is provided, values without defaults are undefined', () => { | ||
| const res = setDefaultsAndCoerceTypes({}); | ||
| // validation should return an error in this case, validating defaults though | ||
| expect(res).toMatchObject({ | ||
| count: 20, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('handleQueryParameters', () => { | ||
| it('returns errors if invalid query parameters', () => { | ||
| const params: GlobalRecsQueryParameterStrings = { | ||
| count: '-1', | ||
| }; | ||
|
|
||
| const error = handleQueryParameters(params); | ||
| assert(error instanceof BFFFxError); | ||
| const errors = JSON.parse(error.stringResponse); | ||
| expect(errors).toEqual( | ||
| expect.objectContaining<APIErrorResponse>({ | ||
| errors: expect.arrayContaining<Array<APIError>>([ | ||
| expect.objectContaining<APIError>({ | ||
| status: '400', | ||
| title: 'Bad Request', | ||
| }), | ||
| ]), | ||
| }) | ||
| ); | ||
| }); | ||
|
Comment on lines
+46
to
+56
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! Wondering if we can use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I copy-pasted these, so I'm not 100% sure, but I believe that |
||
|
|
||
| it('returns GraphQL query variables on success', () => { | ||
| const params: GlobalRecsQueryParameterStrings = { | ||
| count: faker.datatype.number({ min: 1, max: 30 }).toString(), | ||
| locale_lang: 'fr', | ||
| region: 'FR', | ||
| }; | ||
|
|
||
| const variables = handleQueryParameters(params); | ||
| expect(variables).toStrictEqual( | ||
| expect.objectContaining<RecommendationsQueryVariables>({ | ||
| count: parseInt(params.count, 10), | ||
| locale: params.locale_lang, | ||
| region: params.region, | ||
| }) | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have little context around this but if we mean to represent these types as Typescript types then we'll have to use
numberfor all of them 🤔Unless this is just for the openapi schema?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this is an openapi schema, which distinguishes between integers and floating-point numbers.