Skip to content

Commit

Permalink
#34: misc typescript workarounds to build, need improvement later
Browse files Browse the repository at this point in the history
  • Loading branch information
espen42 committed Sep 14, 2021
1 parent 2ef8e32 commit 86d0731
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
6 changes: 4 additions & 2 deletions src/components/BasePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ export type BasePageProps = {


const errorPageSelector = {
404: Custom404,
500: Custom500
'404': Custom404,
'500': Custom500
}

const BasePage = ({type, data, error}: BasePageProps) => {
if (error) {
// @ts-ignore
const ErrorPage = errorPageSelector[error.code] || CustomError;
return <ErrorPage {...error}/>;
}
Expand All @@ -44,6 +45,7 @@ const BasePage = ({type, data, error}: BasePageProps) => {
return null;
}

// @ts-ignore
const SelectedPage = pageSelector[type] || DefaultPage;
return <SelectedPage {...data} />;
};
Expand Down
3 changes: 2 additions & 1 deletion src/components/ClientSideBasePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ type Props = {

const ClientSideBasePage = ({branch, fetchContent}: Props) => {

const [props, setProps] = useState({error: null, content: {}, fetching: true});
const [props, setProps] = useState({error: undefined, content: {}, fetching: true});

const router = useRouter();
const contentPath = router.query.contentPath;

useEffect(
() => {

// @ts-ignore
const refresh = async (contentPath) => {
setProps(props => ({...props, fetching: true}));

Expand Down
2 changes: 1 addition & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function Home() {
this app. Go to the <pre style={{display: 'inline'}}>_path</pre> of a content item:
</p>
<p>/_draft/<em>&lt;site/path/to/item&gt;</em></p>
<p>Of course, just replace '_draft' with '_master' to view published content.</p>
<p>Of course, just replace `&apos;_draft`&apos; with `&apos;_master`&apos; to view published content.</p>
</section>

<section>
Expand Down
22 changes: 19 additions & 3 deletions src/shared/data/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import queryKey from "./queryKey";

// Shape of content base-data API body
export type ContentApiBaseBody = {
query?: string, // Override the default base-data query
variables?: { // GraphQL variables inserted into the query
path?: string, // Full content item _path
}
};

const fetchFromApi = async (
apiUrl: string,
Expand Down Expand Up @@ -53,21 +60,29 @@ const fetchFromApi = async (
return json;
};

export const fetchGuillotine = async <T>(apiUrl, body, key, path, requiredMethodKeyFromQuery): Promise<T> => {
export const fetchGuillotine = async <T>(
apiUrl: string,
body: ContentApiBaseBody,
key: string,
path: string,
requiredMethodKeyFromQuery?: string
): Promise<T> => {
if (typeof body.query !== 'string' || !body.query.trim()) {
// @ts-ignore
return await {
error: {
code: 400,
message: `Invalid or missing query. JSON.stringify(query) = ${JSON.stringify(body.query)}`
}
}
};
}

const result = await fetchFromApi(
apiUrl,
body
)
.then(json => {
let errors = (json || {}).errors;
let errors: any[] = (json || {}).errors;

if (errors) {
if (!Array.isArray(errors)) {
Expand All @@ -78,6 +93,7 @@ export const fetchGuillotine = async <T>(apiUrl, body, key, path, requiredMethod
console.error(error);
});

// @ts-ignore
return {
error: {
code: 500,
Expand Down
16 changes: 6 additions & 10 deletions src/shared/data/fetchContent.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import { getGuillotineUrlDraft, getGuillotineUrlMaster } from "../../enonic-connection-config";

import {fetchGuillotine} from "./data";
import {ContentApiBaseBody, fetchGuillotine} from "./data";

import getQueryMethodKey from './queryKey';

import META_QUERY, {Meta} from "./queries/_getMetaData";
import { LOW_PERFORMING_DEFAULT_QUERY } from "./queries/_getDefaultData";

// Shape of content base-data API body
type ContentApiBaseBody = {
query?: string, // Override the default base-data query
variables?: { // GraphQL variables inserted into the query
path?: string, // Full content item _path
}
};


type Result = {
error?: {
Expand Down Expand Up @@ -97,7 +91,7 @@ const getCleanContentPathArrayOrError400 = (contentPath: string | string[]): str
return contentPathArray;
}

const verifyBranchOrError400 = (branch) => {
const verifyBranchOrError400 = (branch: Branch) => {
if (['draft', 'master'].indexOf(branch) === -1) {
throw Error(JSON.stringify({
code: 400,
Expand Down Expand Up @@ -156,7 +150,7 @@ const buildContentFetcher = ({querySelector, variablesGetterSelector, firstMetho

const defaultGetVariables: VariablesGetterFunc = (path) => ({ path });

const getQueryAndVariables = (type, path) => {
const getQueryAndVariables = (type: string, path: string) => {
// @ts-ignore
let query = querySelector[type];
// @ts-ignore
Expand Down Expand Up @@ -212,6 +206,7 @@ const buildContentFetcher = ({querySelector, variablesGetterSelector, firstMetho
} = metaResult.meta || {};

if (!type) {
// @ts-ignore
return await {
error: {
code: 500,
Expand All @@ -222,6 +217,7 @@ const buildContentFetcher = ({querySelector, variablesGetterSelector, firstMetho

const {query, variables} = getQueryAndVariables(type, path);
if (!query.trim()) {
// @ts-ignore
return await {
error: {
code: 400,
Expand Down
9 changes: 5 additions & 4 deletions src/shared/data/queryKey.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const PATTERN = /\{\s*guillotine\s*\{\s*(.+?)\s*[{(]/;

const getQueryKey = (query) => {
const getQueryKey = (query: string): string => {
try {
// @ts-ignore
const mainQueryKey = query.match(PATTERN)[1];
if (!mainQueryKey) {
throw Error("Regex match group 1 is empty.")
Expand All @@ -13,10 +14,10 @@ const getQueryKey = (query) => {
}
}

const queryMethodKeyCache = {};
const queryMethodKeyCache: {[key: string]: string} = {};

const getQueryMethodKey = (contentType, query) => {
let methodKeyFromQuery = queryMethodKeyCache[contentType];
const getQueryMethodKey = (contentType: string, query: string) => {
let methodKeyFromQuery: string = queryMethodKeyCache[contentType];
if (!methodKeyFromQuery) {
methodKeyFromQuery = getQueryKey(query);
if (methodKeyFromQuery) {
Expand Down

0 comments on commit 86d0731

Please sign in to comment.