Skip to content

Commit

Permalink
feat: [MC-1035] Pass through enableRankByRegion (#75)
Browse files Browse the repository at this point in the history
* fix: openapi-typescript failed to run

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension .mts for scripts/generateOpenAPITypes.mts

* feat: pass enableRankingByRegion to recommendation-api

* fix: pass enableRankingByRegion to query
  • Loading branch information
mmiermans authored May 20, 2024
1 parent 16b2275 commit 35d3588
Show file tree
Hide file tree
Showing 12 changed files with 413 additions and 143 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
extends: ['@pocket-tools/eslint-config'],
ignorePatterns: ['src/generated/**'],
};
7 changes: 7 additions & 0 deletions openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,13 @@ paths:
description: This region string is Fx domain language, and built from Fx expectations. Parameter values are not case sensitive. See [Firefox Home & New Tab Regional Differences](https://mozilla-hub.atlassian.net/wiki/spaces/FPS/pages/80448805/Regional+Differences).
schema:
type: string
- name: enableRankingByRegion
in: query
required: false
description: Returns recommendations specific to the region if set to 1.
schema:
type: integer
enum: [0, 1]
responses:
'200':
description: OK
Expand Down
81 changes: 48 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build": "rm -rf dist && tsc",
"codegen": "npm run codegen:graphql-types",
"codegen:graphql-types": "graphql-codegen",
"codegen:openapi-types": "npm run lint-openapi && ts-node-esm -P scripts/tsconfig.openapi-typescript.json scripts/generateOpenAPITypes.mts",
"codegen:openapi-types": "openapi-typescript ./openapi.yml -o ./src/generated/openapi/types.ts",
"docs": "redocly preview-docs openapi.yml",
"watch": "tsc -w & nodemon",
"start": "node dist/main.js",
Expand Down Expand Up @@ -68,7 +68,7 @@
"jest": "29.7.0",
"nock": "13.2.4",
"nodemon": "2.0.20",
"openapi-typescript": "^6.0.3",
"openapi-typescript": "^6.7.5",
"supertest": "6.3.3",
"ts-jest": "29.1.2",
"ts-node": "^10.9.2"
Expand Down
12 changes: 0 additions & 12 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,3 @@
These files are scripts intended to be run during local development, or during CI/CD.

These are not transpiled into `dist`, and cannot be executed in production environments.

## generateOpenAPITypes.mts

To execute this script:

```bash
npm run codegen
```

This script generates `src/generated/openapi/types.ts`. This requires a separate tsconfig that permits the usage of ESModules, which lives in `tsconfig.openapi-typescript.json`.

This script does not validate `openapi.yml`. It is recommended to lint that file after changing its contents.
24 changes: 0 additions & 24 deletions scripts/generateOpenAPITypes.mts

This file was deleted.

11 changes: 0 additions & 11 deletions scripts/tsconfig.openapi-typescript.json

This file was deleted.

45 changes: 45 additions & 0 deletions src/api/desktop/recommendations/inputs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ describe('input.ts recommendations query parameters', () => {
});
});

it('sets enableRankingByRegion to false if no default is provided', () => {
const res = setDefaultsAndCoerceTypes({});
// validation should return an error in this case, validating defaults though
expect(res.enableRankingByRegion).toStrictEqual(false);
});

describe('handleQueryParameters', () => {
it('returns errors if invalid query parameters', () => {
const params: RecommendationsQueryParameterStrings = {
Expand All @@ -198,6 +204,44 @@ describe('input.ts recommendations query parameters', () => {
);
});

it('returns enableRankingByRegion as true when set to 1', () => {
const params: RecommendationsQueryParameterStrings = {
count: faker.datatype.number({ min: 1, max: 30 }).toString(),
locale: 'fr',
region: 'FR',
enableRankingByRegion: '1',
};

const variables = handleQueryParameters(params);
expect(variables).toStrictEqual(
expect.objectContaining<NewTabRecommendationsQueryVariables>({
count: parseInt(params.count, 10),
locale: params.locale,
region: params.region,
enableRankingByRegion: true,
})
);
});

it('returns enableRankingByRegion as false when set to 0', () => {
const params: RecommendationsQueryParameterStrings = {
count: faker.datatype.number({ min: 1, max: 30 }).toString(),
locale: 'fr',
region: 'FR',
enableRankingByRegion: '0',
};

const variables = handleQueryParameters(params);
expect(variables).toStrictEqual(
expect.objectContaining<NewTabRecommendationsQueryVariables>({
count: parseInt(params.count, 10),
locale: params.locale,
region: params.region,
enableRankingByRegion: false,
})
);
});

it('returns GraphQL query variables on success', () => {
const params: RecommendationsQueryParameterStrings = {
count: faker.datatype.number({ min: 1, max: 30 }).toString(),
Expand All @@ -211,6 +255,7 @@ describe('input.ts recommendations query parameters', () => {
count: parseInt(params.count, 10),
locale: params.locale,
region: params.region,
enableRankingByRegion: false,
})
);
});
Expand Down
6 changes: 4 additions & 2 deletions src/api/desktop/recommendations/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type RecommendationsQueryParameterStrings = Partial<
* This type captures that uncertainty.
*/
type PreValidatedQueryParameters = {
enableRankingByRegion?: boolean;
count: number;
locale?: string;
region?: string;
Expand Down Expand Up @@ -120,6 +121,7 @@ export const setDefaultsAndCoerceTypes = (
return {
...withDefaults,
count: parseInt(withDefaults.count, 10),
enableRankingByRegion: withDefaults.enableRankingByRegion == '1',
};
};

Expand All @@ -132,7 +134,7 @@ export const setDefaultsAndCoerceTypes = (
*/
export const validate = (
query: PreValidatedQueryParameters
): BFFFxErrorInstanceType | RecommendationsQueryParameters => {
): BFFFxErrorInstanceType | PreValidatedQueryParameters => {
// errorDetails is empty if all fields are valid
const errorDetails = [
isValidCount(query.count),
Expand Down Expand Up @@ -160,7 +162,7 @@ export const validate = (
return error;
}

return query as RecommendationsQueryParameters;
return query as PreValidatedQueryParameters;
};

/**
Expand Down
Loading

0 comments on commit 35d3588

Please sign in to comment.