Skip to content
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

chore(gatsby): Convert redux/reducers/redirects to typescript #22810

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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`, () => {
Expand All @@ -15,7 +17,7 @@ describe(`redirects`, () => {
},
}

let state = reducer(undefined, action)
const state = reducer(undefined, action)

expect(state).toEqual([
{
Expand All @@ -34,7 +36,7 @@ describe(`redirects`, () => {
},
}

let state = reducer(undefined, action)
const state = reducer(undefined, action)

expect(state).toEqual([
{
Expand Down Expand Up @@ -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 },
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby/src/redux/reducers/index.js
Original file line number Diff line number Diff line change
@@ -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`

Expand Down Expand Up @@ -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`),
Expand Down
44 changes: 0 additions & 44 deletions packages/gatsby/src/redux/reducers/redirects.js

This file was deleted.

46 changes: 46 additions & 0 deletions packages/gatsby/src/redux/reducers/redirects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import _ from "lodash"
import { IGatsbyState, IRedirect, ICreateRedirectAction } from "../types"

const redirects = new Map<string, IRedirect[]>()

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
}
}
16 changes: 15 additions & 1 deletion packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -303,3 +312,8 @@ export interface IRemoveStaleJobAction {
traceId?: string
payload: { contentDigest: string }
}

export interface ICreateRedirectAction {
type: `CREATE_REDIRECT`
payload: IRedirect
}