Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #229 from githubnext/mr/update-onrequestgithubdata
Browse files Browse the repository at this point in the history
feat: use octokit to make request
  • Loading branch information
Jake Donham authored Dec 6, 2022
2 parents d1ad528 + 2ed28d9 commit 93837a5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
31 changes: 29 additions & 2 deletions components/repo-detail/use-block-frame-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import { AppContext } from "context";
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();

Expand Down Expand Up @@ -50,6 +53,22 @@ const onRequestGitHubData = async (
return resObject;
};

export async function onRequestGitHubEndpoint<
Endpoint extends keyof Endpoints,
EndpointParameters extends Endpoints[Endpoint]["parameters"]
>(
route: Endpoint,
parameters: EndpointParameters & RequestParameters,
octokit: Octokit
) {
const requestOptions = endpoint(route, parameters);
if (requestOptions.method !== "GET") {
throw new Error("Only GET requests are supported");
}
const res = await octokit.request(requestOptions);
return res.data;
}

type BlockFrame = {
window: Window;
origin: string;
Expand Down Expand Up @@ -550,7 +569,7 @@ function useBlockFrameMessages({
const { devServerInfo } = appContext;
const queryClient = useQueryClient();
const router = useRouter();
const { token } = queryClient.getDefaultOptions().queries
const { token, octokit } = queryClient.getDefaultOptions().queries
.meta as BlocksQueryMeta;

const blockFrames = useRef<BlockFrame[]>([]);
Expand Down Expand Up @@ -622,6 +641,15 @@ function useBlockFrameMessages({
});

// handle Block callback functions by name
case "onRequestGitHubEndpoint":
return handleResponse(
onRequestGitHubEndpoint(
data.payload.route,
data.payload.parameters,
octokit
),
responseParams
);
case "onRequestGitHubData":
return handleResponse(
onRequestGitHubData(
Expand All @@ -632,7 +660,6 @@ function useBlockFrameMessages({
),
responseParams
);

case "onRequestBlocksRepos":
return handleResponse(
queryClient.fetchQuery(
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 21 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 93837a5

Please sign in to comment.