From 2ed28d9b09f637abd3498478ae099ec7bfa4b8cc Mon Sep 17 00:00:00 2001 From: Matt Rothenberg Date: Tue, 6 Dec 2022 09:28:01 +0100 Subject: [PATCH] implement endpoint construction logic --- .../repo-detail/use-block-frame-messages.ts | 26 +++++++++++++------ package.json | 2 ++ yarn.lock | 21 +++++++++++++++ 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/components/repo-detail/use-block-frame-messages.ts b/components/repo-detail/use-block-frame-messages.ts index ae7b2800..e913706a 100644 --- a/components/repo-detail/use-block-frame-messages.ts +++ b/components/repo-detail/use-block-frame-messages.ts @@ -21,6 +21,8 @@ import { Context, UpdatedContents } from "./index"; import axios from "axios"; import makeBranchPath from "utils/makeBranchPath"; import { Octokit } from "@octokit/rest"; +import { Endpoints, OctokitResponse, RequestParameters } from "@octokit/types"; +import { endpoint } from "@octokit/endpoint"; const { publicRuntimeConfig } = getConfig(); @@ -51,17 +53,21 @@ const onRequestGitHubData = async ( return resObject; }; -const onRequestGitHubEndpoint = async ( - params: { method: string; url: string }, +export async function onRequestGitHubEndpoint< + Endpoint extends keyof Endpoints, + EndpointParameters extends Endpoints[Endpoint]["parameters"] +>( + route: Endpoint, + parameters: EndpointParameters & RequestParameters, octokit: Octokit -) => { - if (params.method !== "GET") { +) { + const requestOptions = endpoint(route, parameters); + if (requestOptions.method !== "GET") { throw new Error("Only GET requests are supported"); } - - const res = await octokit.request(params.url); + const res = await octokit.request(requestOptions); return res.data; -}; +} type BlockFrame = { window: Window; @@ -637,7 +643,11 @@ function useBlockFrameMessages({ // handle Block callback functions by name case "onRequestGitHubEndpoint": return handleResponse( - onRequestGitHubEndpoint(data.payload, octokit), + onRequestGitHubEndpoint( + data.payload.route, + data.payload.parameters, + octokit + ), responseParams ); case "onRequestGitHubData": diff --git a/package.json b/package.json index 3111c017..72930953 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,9 @@ "@githubnext/blocks": "^2.0.7", "@microsoft/applicationinsights-web": "^2.8.7", "@octokit/auth-app": "^3.6.1", + "@octokit/endpoint": "^7.0.3", "@octokit/rest": "^18.12.0", + "@octokit/types": "^8.0.0", "@panva/hkdf": "^1.0.2", "@primer/octicons-react": "^17.1.0", "@primer/react": "^35.5.0", diff --git a/yarn.lock b/yarn.lock index 947914b4..135b84bc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -754,6 +754,15 @@ is-plain-object "^5.0.0" universal-user-agent "^6.0.0" +"@octokit/endpoint@^7.0.3": + version "7.0.3" + resolved "https://registry.yarnpkg.com/@octokit/endpoint/-/endpoint-7.0.3.tgz#0b96035673a9e3bedf8bab8f7335de424a2147ed" + integrity sha512-57gRlb28bwTsdNXq+O3JTQ7ERmBTuik9+LelgcLIVfYwf235VHbN9QNo4kXExtp/h8T423cR5iJThKtFYxC7Lw== + dependencies: + "@octokit/types" "^8.0.0" + is-plain-object "^5.0.0" + universal-user-agent "^6.0.0" + "@octokit/graphql@^4.5.8": version "4.8.0" resolved "https://registry.yarnpkg.com/@octokit/graphql/-/graphql-4.8.0.tgz#664d9b11c0e12112cbf78e10f49a05959aa22cc3" @@ -784,6 +793,11 @@ resolved "https://registry.yarnpkg.com/@octokit/openapi-types/-/openapi-types-11.2.0.tgz#b38d7fc3736d52a1e96b230c1ccd4a58a2f400a6" integrity sha512-PBsVO+15KSlGmiI8QAzaqvsNlZlrDlyAJYcrXBCvVUxCp7VnXjkwPoFHgjEJXx3WF9BAwkA6nfCUA7i9sODzKA== +"@octokit/openapi-types@^14.0.0": + version "14.0.0" + resolved "https://registry.yarnpkg.com/@octokit/openapi-types/-/openapi-types-14.0.0.tgz#949c5019028c93f189abbc2fb42f333290f7134a" + integrity sha512-HNWisMYlR8VCnNurDU6os2ikx0s0VyEjDYHNS/h4cgb8DeOxQ0n72HyinUtdDVxJhFy3FWLGl0DJhfEWk3P5Iw== + "@octokit/plugin-paginate-rest@^2.16.8": version "2.17.0" resolved "https://registry.yarnpkg.com/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-2.17.0.tgz#32e9c7cab2a374421d3d0de239102287d791bce7" @@ -842,6 +856,13 @@ dependencies: "@octokit/openapi-types" "^11.2.0" +"@octokit/types@^8.0.0": + version "8.0.0" + resolved "https://registry.yarnpkg.com/@octokit/types/-/types-8.0.0.tgz#93f0b865786c4153f0f6924da067fe0bb7426a9f" + integrity sha512-65/TPpOJP1i3K4lBJMnWqPUJ6zuOtzhtagDvydAWbEXpbFYA0oMKKyLb95NFZZP0lSh/4b6K+DQlzvYQJQQePg== + dependencies: + "@octokit/openapi-types" "^14.0.0" + "@panva/hkdf@^1.0.1": version "1.0.1" resolved "https://registry.yarnpkg.com/@panva/hkdf/-/hkdf-1.0.1.tgz#ed0da773bd5f794d0603f5a5b5cee6d2354e5660"