refactor: convert function expressions to declarations (#4926)

This commit is contained in:
Vladislav Shkodin
2021-02-08 20:01:21 +02:00
committed by GitHub
parent c0236536dd
commit 141a2eba56
241 changed files with 3444 additions and 2933 deletions

View File

@ -38,11 +38,11 @@ class RateLimitError extends Error {
}
}
export const requestWithBackoff = async (
export async function requestWithBackoff(
api: API,
req: ApiRequest,
attempt = 1,
): Promise<Response> => {
): Promise<Response> {
if (api.rateLimiter) {
await api.rateLimiter.acquire();
}
@ -92,14 +92,14 @@ export const requestWithBackoff = async (
return requestWithBackoff(api, req, attempt + 1);
}
}
};
}
export const readFile = async (
export async function readFile(
id: string | null | undefined,
fetchContent: () => Promise<string | Blob>,
localForage: LocalForage,
isText: boolean,
) => {
) {
const key = id ? (isText ? `gh.${id}` : `gh.${id}.blob`) : null;
const cached = key ? await localForage.getItem<string | Blob>(key) : null;
if (cached) {
@ -111,20 +111,22 @@ export const readFile = async (
await localForage.setItem(key, content);
}
return content;
};
}
export type FileMetadata = {
author: string;
updatedOn: string;
};
const getFileMetadataKey = (id: string) => `gh.${id}.meta`;
function getFileMetadataKey(id: string) {
return `gh.${id}.meta`;
}
export const readFileMetadata = async (
export async function readFileMetadata(
id: string | null | undefined,
fetchMetadata: () => Promise<FileMetadata>,
localForage: LocalForage,
) => {
) {
const key = id ? getFileMetadataKey(id) : null;
const cached = key && (await localForage.getItem<FileMetadata>(key));
if (cached) {
@ -136,7 +138,7 @@ export const readFileMetadata = async (
await localForage.setItem<FileMetadata>(key, metadata);
}
return metadata;
};
}
/**
* Keywords for inferring a status that will provide a deploy preview URL.
@ -148,12 +150,12 @@ const PREVIEW_CONTEXT_KEYWORDS = ['deploy'];
* deploy preview. Checks for an exact match against `previewContext` if given,
* otherwise checks for inclusion of a value from `PREVIEW_CONTEXT_KEYWORDS`.
*/
export const isPreviewContext = (context: string, previewContext: string) => {
export function isPreviewContext(context: string, previewContext: string) {
if (previewContext) {
return context === previewContext;
}
return PREVIEW_CONTEXT_KEYWORDS.some(keyword => context.includes(keyword));
};
}
export enum PreviewState {
Other = 'other',
@ -164,20 +166,20 @@ export enum PreviewState {
* Retrieve a deploy preview URL from an array of statuses. By default, a
* matching status is inferred via `isPreviewContext`.
*/
export const getPreviewStatus = (
export function getPreviewStatus(
statuses: {
context: string;
target_url: string;
state: PreviewState;
}[],
previewContext: string,
) => {
) {
return statuses.find(({ context }) => {
return isPreviewContext(context, previewContext);
});
};
}
const getConflictingBranches = (branchName: string) => {
function getConflictingBranches(branchName: string) {
// for cms/posts/post-1, conflicting branches are cms/posts, cms
const parts = branchName.split('/');
parts.pop();
@ -188,13 +190,13 @@ const getConflictingBranches = (branchName: string) => {
}, [] as string[]);
return conflictingBranches;
};
}
export const throwOnConflictingBranches = async (
export async function throwOnConflictingBranches(
branchName: string,
getBranch: (name: string) => Promise<{ name: string }>,
apiName: string,
) => {
) {
const possibleConflictingBranches = getConflictingBranches(branchName);
const conflictingBranches = await Promise.all(
@ -213,4 +215,4 @@ export const throwOnConflictingBranches = async (
apiName,
);
}
};
}