From a10382d022f34382cd1ba589e992782e59bd5a65 Mon Sep 17 00:00:00 2001 From: Akshit Kr Nagpal Date: Thu, 9 Apr 2020 14:19:42 +0530 Subject: [PATCH] chore(gatsby): Convert redux/reducers/redirects to typescript (#22810) * Convert redux/reducer/redirects to typescript * Fix Tests * Fix Test * Fix * address PR feedback * fix issues in test Co-authored-by: Blaine Kasten --- .../__tests__/{redirects.js => redirects.ts} | 15 ++++-- packages/gatsby/src/redux/reducers/index.js | 3 +- .../gatsby/src/redux/reducers/redirects.js | 44 ------------------ .../gatsby/src/redux/reducers/redirects.ts | 46 +++++++++++++++++++ packages/gatsby/src/redux/types.ts | 16 ++++++- 5 files changed, 73 insertions(+), 51 deletions(-) rename packages/gatsby/src/redux/reducers/__tests__/{redirects.js => redirects.ts} (86%) delete mode 100644 packages/gatsby/src/redux/reducers/redirects.js create mode 100644 packages/gatsby/src/redux/reducers/redirects.ts diff --git a/packages/gatsby/src/redux/reducers/__tests__/redirects.js b/packages/gatsby/src/redux/reducers/__tests__/redirects.ts similarity index 86% rename from packages/gatsby/src/redux/reducers/__tests__/redirects.js rename to packages/gatsby/src/redux/reducers/__tests__/redirects.ts index 605500d55e604..76323bf7035fc 100644 --- a/packages/gatsby/src/redux/reducers/__tests__/redirects.js +++ b/packages/gatsby/src/redux/reducers/__tests__/redirects.ts @@ -1,9 +1,11 @@ +import { ICreateRedirectAction, IRedirect } from "../../types" + let reducer describe(`redirects`, () => { beforeEach(() => { jest.isolateModules(() => { - reducer = require(`../redirects`) + reducer = require(`../redirects`).redirectsReducer }) }) it(`lets you redirect to an internal url`, () => { @@ -15,7 +17,7 @@ describe(`redirects`, () => { }, } - let state = reducer(undefined, action) + const state = reducer(undefined, action) expect(state).toEqual([ { @@ -34,7 +36,7 @@ describe(`redirects`, () => { }, } - let state = reducer(undefined, action) + const state = reducer(undefined, action) expect(state).toEqual([ { @@ -73,7 +75,10 @@ describe(`redirects`, () => { }) it(`prevents duplicate redirects`, () => { - function createRedirect(fromPath, toPath) { + function createRedirect( + fromPath: string, + toPath: string + ): ICreateRedirectAction { return { type: `CREATE_REDIRECT`, payload: { fromPath, toPath }, @@ -92,7 +97,7 @@ describe(`redirects`, () => { }) it(`allows multiple redirects with same "fromPath" but different options`, () => { - function createRedirect(redirect) { + function createRedirect(redirect: IRedirect): ICreateRedirectAction { return { type: `CREATE_REDIRECT`, payload: redirect, diff --git a/packages/gatsby/src/redux/reducers/index.js b/packages/gatsby/src/redux/reducers/index.js index 67067487f1b42..86a4e2509a487 100644 --- a/packages/gatsby/src/redux/reducers/index.js +++ b/packages/gatsby/src/redux/reducers/index.js @@ -1,5 +1,6 @@ const reduxNodes = require(`./nodes`) const lokiNodes = require(`../../db/loki/nodes`).reducer +import { redirectsReducer } from "./redirects" const backend = process.env.GATSBY_DB_NODES || `redux` @@ -59,7 +60,7 @@ module.exports = { jobsV2: require(`./jobsv2`), webpack: require(`./webpack`), webpackCompilationHash: require(`./webpack-compilation-hash`), - redirects: require(`./redirects`), + redirects: redirectsReducer, babelrc: require(`./babelrc`), schemaCustomization: require(`./schema-customization`), themes: require(`./themes`), diff --git a/packages/gatsby/src/redux/reducers/redirects.js b/packages/gatsby/src/redux/reducers/redirects.js deleted file mode 100644 index ed8b072dc98fd..0000000000000 --- a/packages/gatsby/src/redux/reducers/redirects.js +++ /dev/null @@ -1,44 +0,0 @@ -const _ = require(`lodash`) - -const redirects = new Map() - -function exists(newRedirect) { - if (!redirects.has(newRedirect.fromPath)) { - return false - } - - return redirects - .get(newRedirect.fromPath) - .some(redirect => _.isEqual(redirect, newRedirect)) -} - -function add(redirect) { - let samePathRedirects = redirects.get(redirect.fromPath) - - if (!samePathRedirects) { - samePathRedirects = [] - redirects.set(redirect.fromPath, samePathRedirects) - } - - samePathRedirects.push(redirect) -} - -module.exports = (state = [], action) => { - switch (action.type) { - case `CREATE_REDIRECT`: { - const redirect = action.payload - - // Add redirect only if it wasn't yet added to prevent duplicates - if (!exists(redirect)) { - add(redirect) - - state.push(redirect) - } - - return state - } - - default: - return state - } -} diff --git a/packages/gatsby/src/redux/reducers/redirects.ts b/packages/gatsby/src/redux/reducers/redirects.ts new file mode 100644 index 0000000000000..716d0ebe3ed45 --- /dev/null +++ b/packages/gatsby/src/redux/reducers/redirects.ts @@ -0,0 +1,46 @@ +import _ from "lodash" +import { IGatsbyState, IRedirect, ICreateRedirectAction } from "../types" + +const redirects = new Map() + +function exists(newRedirect: IRedirect): boolean { + const fromPathRedirects = redirects.get(newRedirect.fromPath) + + if (!fromPathRedirects) return false + + return fromPathRedirects.some(redirect => _.isEqual(redirect, newRedirect)) +} + +function add(redirect: IRedirect): void { + let samePathRedirects = redirects.get(redirect.fromPath) + + if (!samePathRedirects) { + samePathRedirects = [] + redirects.set(redirect.fromPath, samePathRedirects) + } + + samePathRedirects.push(redirect) +} + +export const redirectsReducer = ( + state: IGatsbyState["redirects"] = [], + action: ICreateRedirectAction +): IGatsbyState["redirects"] => { + switch (action.type) { + case `CREATE_REDIRECT`: { + const redirect = action.payload + + // Add redirect only if it wasn't yet added to prevent duplicates + if (!exists(redirect)) { + add(redirect) + + state.push(redirect) + } + + return state + } + + default: + return state + } +} diff --git a/packages/gatsby/src/redux/types.ts b/packages/gatsby/src/redux/types.ts index dce585b7f6970..e814c1053494e 100644 --- a/packages/gatsby/src/redux/types.ts +++ b/packages/gatsby/src/redux/types.ts @@ -6,6 +6,15 @@ type SystemPath = string type Identifier = string type StructuredLog = any // TODO this should come from structured log interface +export interface IRedirect { + fromPath: string + toPath: string + isPermanent?: boolean + redirectInBrowser?: boolean + // Users can add anything to this createRedirect API + [key: string]: any +} + export enum ProgramStatus { BOOTSTRAP_FINISHED = `BOOTSTRAP_FINISHED`, BOOTSTRAP_QUERY_RUNNING_FINISHED = `BOOTSTRAP_QUERY_RUNNING_FINISHED`, @@ -140,7 +149,7 @@ export interface IGatsbyState { } webpack: any // TODO This should be the output from ./utils/webpack.config.js webpackCompilationHash: string - redirects: any[] // TODO + redirects: IRedirect[] babelrc: { stages: { develop: any // TODO @@ -303,3 +312,8 @@ export interface IRemoveStaleJobAction { traceId?: string payload: { contentDigest: string } } + +export interface ICreateRedirectAction { + type: `CREATE_REDIRECT` + payload: IRedirect +}