Skip to content

Commit

Permalink
Typescript main (#88)
Browse files Browse the repository at this point in the history
* Add type annotations for the reducers and state (#84)

Incremental typing. I'm sure not everything is correct, but
it'll improve as we type other parts of superglue. For now,
i'm keeping close parity with what outputted to /dist, which is
why you continue to see defaults on reducers.

* Add action types (#85)

* Add type annotations to utils/url (#87)

Resolves #75

* Add remote and visit type annotations (#86)

This adds remote and visit type annotations. `npm run type-check` doesn't
pass yet, but the output to dist is still acceptable. Its a step in the right direction.

* Update UJS type annotations

* Prettier

* Add type annotations to index.tsx

* Add types to middleware

* Add types to utils/request.ts

* Add types to Nav.tsx

* Add types to superglue index

* Update type annotations on action creators

* Add typing to update fragment actions

* Prettier and update Error

* Add typing for url parse

* Remove any from data

* Add content type

* Remove Anys

* Add type annotations to immutability

* Revert removal of buildstore

* Disable eslint

* Maintain backward compatibility while preparing for redux toolkit

* Fix specs

* Remove babel proptypes

* Remove any action

* Fix depedencies

* Remove extraneous pagekey setting

* Use abstract classes

* Use Record

* Ensure override of url params when using form
  • Loading branch information
jho406 authored Jul 2, 2024
1 parent e374a3b commit 71d78fc
Show file tree
Hide file tree
Showing 23 changed files with 830 additions and 500 deletions.
4 changes: 1 addition & 3 deletions superglue/.babelrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module.exports = {
plugins: [
["transform-react-remove-prop-types", { removeImport: true }]
],
plugins: [],
presets: [
[
"@babel/preset-env",
Expand Down
2 changes: 1 addition & 1 deletion superglue/.prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"semi": false,
"singleQuote": true,
"printWidth": 70,
"printWidth": 80
}
5 changes: 0 additions & 5 deletions superglue/.prettierrc.json

This file was deleted.

51 changes: 42 additions & 9 deletions superglue/lib/action_creators/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isGraft, urlToPageKey, getIn } from '../utils'
import { urlToPageKey, getIn } from '../utils'
import parse from 'url-parse'
import {
SAVE_RESPONSE,
Expand All @@ -9,9 +9,26 @@ import {
UPDATE_FRAGMENTS,
} from '../actions'
import { remote } from './requests'
import {
CopyAction,
VisitResponse,
SaveResponseAction,
UpdateFragmentsAction,
SaveAndProcessPageThunk,
DefermentThunk,
HandleGraftAction,
GraftResponse,
Page,
} from '../types'
export * from './requests'

export function copyPage({ from, to }) {
export function copyPage({
from,
to,
}: {
from: string
to: string
}): CopyAction {
return {
type: COPY_PAGE,
payload: {
Expand All @@ -21,7 +38,13 @@ export function copyPage({ from, to }) {
}
}

export function saveResponse({ pageKey, page }) {
export function saveResponse({
pageKey,
page,
}: {
pageKey: string
page: VisitResponse
}): SaveResponseAction {
pageKey = urlToPageKey(pageKey)

return {
Expand All @@ -33,7 +56,13 @@ export function saveResponse({ pageKey, page }) {
}
}

export function handleGraft({ pageKey, page }) {
export function handleGraft({
pageKey,
page,
}: {
pageKey: string
page: GraftResponse
}): HandleGraftAction {
pageKey = urlToPageKey(pageKey)

return {
Expand All @@ -45,7 +74,7 @@ export function handleGraft({ pageKey, page }) {
}
}

function fetchDeferments(pageKey, defers = []) {
function fetchDeferments(pageKey: string, defers = []): DefermentThunk {
pageKey = urlToPageKey(pageKey)
return (dispatch) => {
const fetches = defers
Expand Down Expand Up @@ -85,7 +114,7 @@ function fetchDeferments(pageKey, defers = []) {
}
}

function updateFragmentsUsing(page) {
function updateFragmentsUsing(page: Page): UpdateFragmentsAction {
const changedFragments = {}
page.fragments.forEach((fragment) => {
const { type, path } = fragment
Expand All @@ -98,13 +127,16 @@ function updateFragmentsUsing(page) {
}
}

export function saveAndProcessPage(pageKey, page) {
export function saveAndProcessPage(
pageKey: string,
page: VisitResponse | GraftResponse
): SaveAndProcessPageThunk {
return (dispatch, getState) => {
pageKey = urlToPageKey(pageKey)

const { defers = [] } = page

if (isGraft(page)) {
if ('action' in page) {
dispatch(handleGraft({ pageKey, page }))
} else {
dispatch(saveResponse({ pageKey, page }))
Expand All @@ -115,7 +147,8 @@ export function saveAndProcessPage(pageKey, page) {
return dispatch(fetchDeferments(pageKey, defers)).then(() => {
if (page.fragments.length > 0) {
const finishedPage = getState().pages[pageKey]
return dispatch(updateFragmentsUsing(finishedPage))
dispatch(updateFragmentsUsing(finishedPage))
return Promise.resolve()
}
})
} else {
Expand Down
97 changes: 52 additions & 45 deletions superglue/lib/action_creators/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,48 @@ import {
SUPERGLUE_ERROR,
} from '../actions'
import { copyPage, saveAndProcessPage } from './index'

function beforeVisit(payload) {
import {
BeforeVisit,
BeforeFetch,
FetchArgs,
BeforeRemote,
HandleError,
RemoteProps,
VisitProps,
VisitResponse,
SuperglueState,
Meta,
MetaThunk,
} from '../types'

function beforeVisit(payload: {
fetchArgs: FetchArgs
currentPageKey: string
}): BeforeVisit {
return {
type: BEFORE_VISIT,
payload,
}
}

function beforeRemote(payload) {
function beforeRemote(payload: {
fetchArgs: FetchArgs
currentPageKey: string
}): BeforeRemote {
return {
type: BEFORE_REMOTE,
payload,
}
}

function beforeFetch(payload) {
function beforeFetch(payload: { fetchArgs: FetchArgs }): BeforeFetch {
return {
type: BEFORE_FETCH,
payload,
}
}

function handleError(err) {
function handleError(err: Error): HandleError {
return {
type: SUPERGLUE_ERROR,
payload: {
Expand All @@ -45,36 +64,45 @@ function handleError(err) {
}
}

function handleFetchErr(err, fetchArgs, dispatch) {
function handleFetchErr(err, fetchArgs, dispatch): never {
err.fetchArgs = fetchArgs
err.url = fetchArgs[0]
err.pageKey = urlToPageKey(fetchArgs[0])
dispatch(handleError(err))
throw err
}

function buildMeta(pageKey, page, state) {
function buildMeta(
pageKey: string,
page: VisitResponse,
state: SuperglueState,
rsp: Response,
fetchArgs: FetchArgs
): Meta {
const { assets: prevAssets } = state
const { assets: nextAssets } = page

return {
pageKey,
page,
redirected: rsp.redirected,
rsp,
fetchArgs,
componentIdentifier: page.componentIdentifier,
needsRefresh: needsRefresh(prevAssets, nextAssets),
}
}

export function remote(
path,
path: string,
{
method = 'GET',
headers,
body = '',
body,
pageKey,
beforeSave = (prevPage, receivedPage) => receivedPage,
} = {}
) {
}: RemoteProps = {}
): MetaThunk {
path = withoutBusters(path)
pageKey = pageKey && urlToPageKey(pageKey)

Expand All @@ -95,22 +123,12 @@ export function remote(
.then(({ rsp, json }) => {
const { superglue, pages = {} } = getState()

const meta = {
...buildMeta(pageKey, json, superglue),
redirected: rsp.redirected,
rsp,
fetchArgs,
}

const meta = buildMeta(pageKey, json, superglue, rsp, fetchArgs)
const willReplaceCurrent = pageKey == currentPageKey
const existingId = pages[currentPageKey]?.componentIdentifier
const receivedId = json.componentIdentifier

if (
willReplaceCurrent &&
!!existingId &&
existingId != receivedId
) {
if (willReplaceCurrent && !!existingId && existingId != receivedId) {
console.warn(
`You're about replace an existing page located at pages["${currentPageKey}"]
that has the componentIdentifier "${existingId}" with the contents of a
Expand All @@ -126,12 +144,7 @@ Consider using data-sg-visit, the visit function, or redirect_back.`
}

const page = beforeSave(pages[pageKey], json)
return dispatch(saveAndProcessPage(pageKey, page)).then(
() => {
meta.pageKey = pageKey
return meta
}
)
return dispatch(saveAndProcessPage(pageKey, page)).then(() => meta)
})
.catch((e) => handleFetchErr(e, fetchArgs, dispatch))
}
Expand All @@ -144,16 +157,16 @@ let lastVisitController = {
}

export function visit(
path,
path: string,
{
method = 'GET',
headers,
body = '',
body,
placeholderKey,
beforeSave = (prevPage, receivedPage) => receivedPage,
revisit = false,
} = {}
) {
}: VisitProps = {}
): MetaThunk {
path = withoutBusters(path)
let pageKey = urlToPageKey(path)

Expand Down Expand Up @@ -200,12 +213,8 @@ export function visit(
.then(({ rsp, json }) => {
const { superglue, pages = {} } = getState()

const meta = {
...buildMeta(pageKey, json, superglue),
redirected: rsp.redirected,
rsp,
fetchArgs,
}
const meta = buildMeta(pageKey, json, superglue, rsp, fetchArgs)

const isGet = fetchArgs[1].method === 'GET'

meta.suggestedAction = 'push'
Expand Down Expand Up @@ -233,12 +242,10 @@ export function visit(
}

const page = beforeSave(pages[pageKey], json)
return dispatch(saveAndProcessPage(pageKey, page)).then(
() => {
meta.pageKey = pageKey
return meta
}
)
return dispatch(saveAndProcessPage(pageKey, page)).then(() => {
meta.pageKey = pageKey
return meta
})
})
.catch((e) => handleFetchErr(e, fetchArgs, dispatch))
}
Expand Down
Loading

0 comments on commit 71d78fc

Please sign in to comment.