From ae80df24b74098aa73fcb59471dbea286d5e5566 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Tue, 20 Oct 2020 11:00:11 +0100 Subject: [PATCH] Add typings for API --- client/src/api/admin.js | 30 ---------- client/src/api/admin.ts | 59 +++++++++++++++++++ client/src/components/Explorer/actions.ts | 6 +- .../src/components/Explorer/reducers/nodes.ts | 16 +---- 4 files changed, 63 insertions(+), 48 deletions(-) delete mode 100644 client/src/api/admin.js create mode 100644 client/src/api/admin.ts diff --git a/client/src/api/admin.js b/client/src/api/admin.js deleted file mode 100644 index d299d882ba..0000000000 --- a/client/src/api/admin.js +++ /dev/null @@ -1,30 +0,0 @@ -import { get } from '../api/client'; - -import { ADMIN_API } from '../config/wagtailConfig'; - - -export const getPage = (id) => { - const url = `${ADMIN_API.PAGES}${id}/`; - - return get(url); -}; - -export const getPageChildren = (id, options = {}) => { - let url = `${ADMIN_API.PAGES}?child_of=${id}&for_explorer=1`; - - if (options.fields) { - url += `&fields=${global.encodeURIComponent(options.fields.join(','))}`; - } - - if (options.onlyWithChildren) { - url += '&has_children=1'; - } - - if (options.offset) { - url += `&offset=${options.offset}`; - } - - url += ADMIN_API.EXTRA_CHILDREN_PARAMETERS; - - return get(url); -}; diff --git a/client/src/api/admin.ts b/client/src/api/admin.ts new file mode 100644 index 0000000000..9307283ecd --- /dev/null +++ b/client/src/api/admin.ts @@ -0,0 +1,59 @@ +import { get } from '../api/client'; + +import { ADMIN_API } from '../config/wagtailConfig'; + +export interface WagtailPageAPI { + id: number; + meta: { + status: { + status: string; + live: boolean; + /* eslint-disable-next-line camelcase */ + has_unpublished_changes: boolean; + } + children: any; + }; + /* eslint-disable-next-line camelcase */ + admin_display_title?: string; +} + +interface WagtailPageListAPI { + meta: { + /* eslint-disable-next-line camelcase */ + total_count: number; + }; + items: WagtailPageAPI[]; +} + +export const getPage: (id: number) => Promise = (id) => { + const url = `${ADMIN_API.PAGES}${id}/`; + + return get(url); +}; + +interface GetPageChildrenOptions { + fields?: string[]; + onlyWithChildren?: boolean; + offset?: number; +} + +type GetPageChildren = (id: number, options: GetPageChildrenOptions) => Promise; +export const getPageChildren: GetPageChildren = (id, options = {}) => { + let url = `${ADMIN_API.PAGES}?child_of=${id}&for_explorer=1`; + + if (options.fields) { + url += `&fields=${window.encodeURIComponent(options.fields.join(','))}`; + } + + if (options.onlyWithChildren) { + url += '&has_children=1'; + } + + if (options.offset) { + url += `&offset=${options.offset}`; + } + + url += ADMIN_API.EXTRA_CHILDREN_PARAMETERS; + + return get(url); +}; diff --git a/client/src/components/Explorer/actions.ts b/client/src/components/Explorer/actions.ts index c6bcd06458..2685f4f1e3 100644 --- a/client/src/components/Explorer/actions.ts +++ b/client/src/components/Explorer/actions.ts @@ -5,7 +5,7 @@ import { createAction } from '../../utils/actions'; import { MAX_EXPLORER_PAGES } from '../../config/wagtailConfig'; import { State as ExplorerState, Action as ExplorerAction } from './reducers/explorer'; -import { State as NodeState, WagtailPageAPI, Action as NodeAction } from './reducers/nodes'; +import { State as NodeState, Action as NodeAction } from './reducers/nodes'; interface State { explorer: ExplorerState, @@ -15,7 +15,7 @@ interface State { type Action = ExplorerAction | NodeAction; type ThunkActionType = ThunkAction; -const getPageSuccess = createAction('GET_PAGE_SUCCESS', (id: number, data: WagtailPageAPI) => ({ id, data })); +const getPageSuccess = createAction('GET_PAGE_SUCCESS', (id: number, data: admin.WagtailPageAPI) => ({ id, data })); const getPageFailure = createAction('GET_PAGE_FAILURE', (id: number, error: Error) => ({ id, error })); /** @@ -32,7 +32,7 @@ function getPage(id: number): ThunkActionType { const getChildrenStart = createAction('GET_CHILDREN_START', (id: number) => ({ id })); const getChildrenSuccess = createAction( 'GET_CHILDREN_SUCCESS', - (id, items: WagtailPageAPI[], meta: any) => ({ id, items, meta }) + (id, items: admin.WagtailPageAPI[], meta: any) => ({ id, items, meta }) ); const getChildrenFailure = createAction('GET_CHILDREN_FAILURE', (id: number, error: Error) => ({ id, error })); diff --git a/client/src/components/Explorer/reducers/nodes.ts b/client/src/components/Explorer/reducers/nodes.ts index 8956392f64..1bb2c2a27f 100644 --- a/client/src/components/Explorer/reducers/nodes.ts +++ b/client/src/components/Explorer/reducers/nodes.ts @@ -1,20 +1,6 @@ +import { WagtailPageAPI } from '../../../api/admin'; import { OPEN_EXPLORER } from './explorer'; -export interface WagtailPageAPI { - id: number; - meta: { - status: { - status: string; - live: boolean; - /* eslint-disable-next-line camelcase */ - has_unpublished_changes: boolean; - } - children: any; - }; - /* eslint-disable-next-line camelcase */ - admin_display_title?: string; -} - export interface PageState extends WagtailPageAPI { isFetching: boolean; isError: boolean;