Skip to content

Commit

Permalink
feat: exists key for update functions
Browse files Browse the repository at this point in the history
fixes #51
  • Loading branch information
gr2m committed Jun 29, 2020
1 parent 5f7f416 commit 365f02c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
33 changes: 23 additions & 10 deletions src/create-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,30 @@ export async function createTree(
// When passed a function, retrieve the content of the file, pass it
// to the function, then return the result
if (typeof value === "function") {
const { data: file } = await octokit.request(
"GET /repos/:owner/:repo/contents/:path",
{
owner: fork,
repo,
ref: latestCommitSha,
path,
}
);
let result;

try {
const { data: file } = await octokit.request(
"GET /repos/:owner/:repo/contents/:path",
{
owner: fork,
repo,
ref: latestCommitSha,
path,
}
);

result = await value(
Object.assign(file, { exists: true }) as UpdateFunctionFile
);
} catch (error) {
if (error.status !== 404) throw error;

// @ts-ignore
result = await value({ exists: false });
}

const result = await value(file as UpdateFunctionFile);
if (result === null || typeof result === "undefined") return;
return valueToTreeObject(octokit, owner, repo, path, result);
}

Expand Down
20 changes: 14 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ export type File = {
encoding: "utf-8" | "base64";
};

export type UpdateFunctionFile = {
size: number;
encoding: "base64";
content: string;
};
export type UpdateFunctionFile =
| {
exists: true;
size: number;
encoding: "base64";
content: string;
}
| {
exists: false;
size: never;
encoding: never;
content: never;
};

export type UpdateFunction = (file: UpdateFunctionFile) => string | File;
export type UpdateFunction = (file: UpdateFunctionFile) => string | File | null;

export type State = {
octokit: Octokit;
Expand Down

0 comments on commit 365f02c

Please sign in to comment.