|
| 1 | +// Import Internal Dependencies |
| 2 | +import { ApiEndpoint } from "../class/ApiEndpoint.ts"; |
| 3 | +import { createApiProxy } from "../class/createApiProxy.ts"; |
| 4 | +import type { |
| 5 | + Tag, |
| 6 | + PullRequest, |
| 7 | + Issue, |
| 8 | + Commit, |
| 9 | + Workflow, |
| 10 | + WorkflowRun, |
| 11 | + Job, |
| 12 | + Artifact, |
| 13 | + WorkflowsResponse, |
| 14 | + WorkflowRunsResponse, |
| 15 | + JobsResponse, |
| 16 | + ArtifactsResponse, |
| 17 | + RequestConfig |
| 18 | +} from "../types.ts"; |
| 19 | + |
| 20 | +type RepoEndpointMethods = { |
| 21 | + tags: () => ApiEndpoint<Tag>; |
| 22 | + pulls: () => ApiEndpoint<PullRequest>; |
| 23 | + issues: () => ApiEndpoint<Issue>; |
| 24 | + commits: () => ApiEndpoint<Commit>; |
| 25 | + workflows: () => ApiEndpoint<Workflow>; |
| 26 | + workflowRuns: (workflowId: string | number) => ApiEndpoint<WorkflowRun>; |
| 27 | + runJobs: (runId: number) => ApiEndpoint<Job>; |
| 28 | + runArtifacts: (runId: number) => ApiEndpoint<Artifact>; |
| 29 | +}; |
| 30 | + |
| 31 | +export type ReposProxy = { |
| 32 | + [owner: string]: { |
| 33 | + [repo: string]: RepoEndpointMethods; |
| 34 | + }; |
| 35 | +}; |
| 36 | + |
| 37 | +function createRepoProxy( |
| 38 | + owner: string, |
| 39 | + repo: string, |
| 40 | + config: RequestConfig = {} |
| 41 | +): RepoEndpointMethods { |
| 42 | + return { |
| 43 | + tags: () => new ApiEndpoint<Tag>(`/repos/${owner}/${repo}/tags`, config), |
| 44 | + pulls: () => new ApiEndpoint<PullRequest>(`/repos/${owner}/${repo}/pulls`, config), |
| 45 | + issues: () => new ApiEndpoint<Issue>(`/repos/${owner}/${repo}/issues`, config), |
| 46 | + commits: () => new ApiEndpoint<Commit>(`/repos/${owner}/${repo}/commits`, config), |
| 47 | + workflows: () => new ApiEndpoint<Workflow>( |
| 48 | + `/repos/${owner}/${repo}/actions/workflows`, |
| 49 | + { ...config, extractor: (raw: WorkflowsResponse) => raw.workflows } |
| 50 | + ), |
| 51 | + workflowRuns: (workflowId: string | number) => new ApiEndpoint<WorkflowRun>( |
| 52 | + `/repos/${owner}/${repo}/actions/workflows/${workflowId}/runs`, |
| 53 | + { ...config, extractor: (raw: WorkflowRunsResponse) => raw.workflow_runs } |
| 54 | + ), |
| 55 | + runJobs: (runId: number) => new ApiEndpoint<Job>( |
| 56 | + `/repos/${owner}/${repo}/actions/runs/${runId}/jobs`, |
| 57 | + { ...config, extractor: (raw: JobsResponse) => raw.jobs } |
| 58 | + ), |
| 59 | + runArtifacts: (runId: number) => new ApiEndpoint<Artifact>( |
| 60 | + `/repos/${owner}/${repo}/actions/runs/${runId}/artifacts`, |
| 61 | + { ...config, extractor: (raw: ArtifactsResponse) => raw.artifacts } |
| 62 | + ) |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +export function createReposProxy(config: RequestConfig = {}): ReposProxy { |
| 67 | + return createApiProxy( |
| 68 | + (owner) => createApiProxy( |
| 69 | + (repo) => createRepoProxy(owner, repo, config) |
| 70 | + ) |
| 71 | + ) as ReposProxy; |
| 72 | +} |
| 73 | + |
| 74 | +export const repos = createReposProxy(); |
0 commit comments