Skip to content

Commit 1689922

Browse files
committed
fix: schema & argument types
1 parent c5d2020 commit 1689922

4 files changed

Lines changed: 33 additions & 25 deletions

File tree

src/github.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import fs from 'node:fs/promises'
22
import path from 'node:path'
33
import { logger } from './logger.js'
4-
import { GitHubRepositoryAPIResponse, InternalLoaderOptions } from './types.js'
4+
import { GitHubRepositoryAPIResponse, InternalLoaderOptionsType } from './types.js'
55

6-
export function getProjectIgnoreFile(options: Pick<InternalLoaderOptions, 'overridesDir'>) {
6+
export function getProjectIgnoreFile(options: Pick<InternalLoaderOptionsType, 'overridesDir'>) {
77
return path.resolve(options.overridesDir, '.projectignore')
88
}
9-
export function getProjectKeepFile(options: Pick<InternalLoaderOptions, 'overridesDir'>) {
9+
export function getProjectKeepFile(options: Pick<InternalLoaderOptionsType, 'overridesDir'>) {
1010
return path.resolve(options.overridesDir, '.projectkeep')
1111
}
1212

@@ -15,7 +15,7 @@ export let projectKeep: string[] = []
1515

1616
export async function fetchRepos(
1717
endpoint: string,
18-
options: InternalLoaderOptions,
18+
options: InternalLoaderOptionsType,
1919
): Promise<GitHubRepositoryAPIResponse[]> {
2020
const repos = []
2121

@@ -30,7 +30,7 @@ export async function fetchRepos(
3030
return repos
3131
}
3232

33-
async function fetchReposPage(endpoint: string, options: InternalLoaderOptions) {
33+
async function fetchReposPage(endpoint: string, options: InternalLoaderOptionsType) {
3434
const headers = getAuthorization(options)
3535
logger.log('Fetching endpoint', endpoint)
3636
const response = await fetch(endpoint, { headers })
@@ -61,12 +61,12 @@ async function loadFileList(file: string): Promise<string[]> {
6161
.filter((x) => x[0] !== '#')
6262
}
6363

64-
export async function reloadOverrides(options: InternalLoaderOptions) {
64+
export async function reloadOverrides(options: InternalLoaderOptionsType) {
6565
projectIgnore = await loadFileList(getProjectIgnoreFile(options))
6666
projectKeep = await loadFileList(getProjectKeepFile(options))
6767
}
6868

69-
export function getAuthorization(options: InternalLoaderOptions): Headers {
69+
export function getAuthorization(options: InternalLoaderOptionsType): Headers {
7070
const headers = new Headers()
7171
headers.set('Authorization', `Bearer ${options.apiToken}`)
7272
return headers

src/loader.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import type { Loader, LoaderContext } from 'astro/loaders'
22
import { reloadOverrides } from './github.js'
33
import { parseJSON as parseDate } from 'date-fns/parseJSON'
44
import { formatISO as formatDate } from 'date-fns/formatISO'
5-
import type { LoaderOptions } from './types.js'
5+
import type { GitHubProjectType, LoaderOptionsType } from './types.js'
66
import { GitHubProjectSchema, InternalLoaderOptions } from './types.js'
77
import { logger } from './logger.js'
88
import { getProjectsList } from './parser.js'
99
import path from 'path'
10+
import { z } from 'zod'
1011

1112
const defaultOverridesDir = path.join(process.cwd(), 'src', 'content', 'project-overrides')
1213

1314
async function reloadProjects(
1415
{ store, meta }: Pick<LoaderContext, 'store' | 'meta'>,
15-
opts: LoaderOptions,
16+
opts: LoaderOptionsType,
1617
) {
1718
logger.enabled = opts.debug ?? false
1819
const lastUpdated = parseDate(
@@ -21,6 +22,7 @@ async function reloadProjects(
2122

2223
const options = InternalLoaderOptions.parse({
2324
debug: false,
25+
force: false,
2426
orgs: [],
2527
overridesDir: defaultOverridesDir,
2628
...opts,
@@ -47,10 +49,10 @@ async function reloadProjects(
4749
* A loader that fetches GitHub repositories for a user and organization(s).
4850
* @param opts The loader options.
4951
*/
50-
export function githubProjectsLoader(opts: LoaderOptions): Loader {
52+
export function githubProjectsLoader(opts: LoaderOptionsType): Loader {
5153
return {
5254
name: 'github-repos-loader',
53-
schema: GitHubProjectSchema,
55+
schema: GitHubProjectSchema as unknown as z.ZodType<GitHubProjectType>,
5456
load: async ({ store, meta, watcher }) => {
5557
await reloadProjects({ store, meta }, opts)
5658

src/parser.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ import path from 'node:path'
33
import yaml from 'yaml'
44
import { parseJSON as parseDate } from 'date-fns/parseJSON'
55
import { formatISO as formatDate } from 'date-fns/formatISO'
6-
import { GitHubProjectSchema, GitHubRepositoryAPIResponse, InternalLoaderOptions } from './types.js'
6+
import {
7+
GitHubProjectSchema,
8+
GitHubProjectType,
9+
GitHubRepositoryAPIResponse,
10+
InternalLoaderOptionsType,
11+
} from './types.js'
712
import { fileExists, parseMarkdown } from './utils.js'
813
import { logger } from './logger.js'
914
import { fetchRepos, getAuthorization, projectIgnore, projectKeep } from './github.js'
1015

1116
export async function getProjectsList(
12-
options: InternalLoaderOptions,
13-
): Promise<GitHubProjectSchema[]> {
17+
options: InternalLoaderOptionsType,
18+
): Promise<GitHubProjectType[]> {
1419
logger.log(`Fetching projects list from GitHub (since: ${formatDate(options.lastUpdated)})`)
1520

1621
const repos = await fetchRepos(
@@ -24,7 +29,7 @@ export async function getProjectsList(
2429
}
2530

2631
logger.log(`Fetched ${repos.length} projects from GitHub`)
27-
const projects: GitHubProjectSchema[] = []
32+
const projects: GitHubProjectType[] = []
2833

2934
for (const repo of repos) {
3035
logger.log(`Processing ${repo.name}`)
@@ -42,6 +47,7 @@ export async function getProjectsList(
4247
order: -repo.stargazers_count,
4348
links: [{ href: repo.html_url, icon: 'logo-github', title: 'GitHub' }],
4449
raw: repo,
50+
featured: false,
4551
})
4652

4753
const overridesFile = path.join(options.overridesDir, `${project.name}.md`)
@@ -52,7 +58,7 @@ export async function getProjectsList(
5258
const lines = allLines.slice(0, allLines.lastIndexOf('---')).join('\n').trim()
5359
try {
5460
// TODO use GitHubProjectSchema.parse
55-
const obj = yaml.parse(lines) as GitHubProjectSchema
61+
const obj = yaml.parse(lines) as GitHubProjectType
5662
for (const link of obj.links ?? []) {
5763
const found = project.links.findIndex((i) => i.href === link.href)
5864
if (found >= 0) {
@@ -97,7 +103,7 @@ export async function getProjectsList(
97103

98104
function projectFilter(
99105
project: GitHubRepositoryAPIResponse,
100-
{ lastUpdated, filter }: InternalLoaderOptions,
106+
{ lastUpdated, filter }: InternalLoaderOptionsType,
101107
): boolean {
102108
if (projectKeep.includes(project.name)) {
103109
return true

src/types.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const LinkSchema = z.object({
1313
/**
1414
* Schema for a link.
1515
*/
16-
export type LinkSchema = {
16+
export type TLinkSchema = {
1717
/** The title of the link. */
1818
title: string
1919
/** The URL the link points to. */
@@ -35,14 +35,14 @@ export const GitHubProjectSchema = z.object({
3535
readmeHtml: z.string().optional(),
3636
order: z.number(),
3737
links: z.array(LinkSchema),
38-
featured: z.boolean().optional().default(false),
38+
featured: z.boolean(),
3939
raw: z.any() as z.ZodType<GitHubRepositoryAPIResponse>,
4040
})
4141

4242
/**
4343
* Schema for a GitHub project.
4444
*/
45-
export type GitHubProjectSchema = {
45+
export type GitHubProjectType = {
4646
/** The name of the GitHub project. */
4747
name: string
4848
/** The title of the GitHub project. */
@@ -60,7 +60,7 @@ export type GitHubProjectSchema = {
6060
/** The order of the GitHub project in a list. */
6161
order: number
6262
/** The links associated with the GitHub project. */
63-
links: LinkSchema[]
63+
links: TLinkSchema[]
6464
/** Whether the GitHub project is featured. */
6565
featured: boolean
6666
/** Raw response from GitHub API, containing all fields. */
@@ -77,22 +77,22 @@ export type GitHubRepositoryAPIResponse = NonNullable<
7777
*/
7878
export const LoaderOptions = z.object({
7979
username: z.string(),
80-
debug: z.boolean().optional(),
80+
debug: z.boolean(),
8181
orgs: z.array(z.string()).optional(),
8282
filter: z
8383
.function()
8484
.args(z.any() as z.ZodType<GitHubRepositoryAPIResponse>)
8585
.returns(z.boolean())
8686
.optional(),
8787
apiToken: z.string(),
88-
force: z.boolean().optional().default(false),
88+
force: z.boolean(),
8989
overridesDir: z.string().optional(),
9090
})
9191

9292
/**
9393
* Schema for loader options.
9494
*/
95-
export type LoaderOptions = {
95+
export type LoaderOptionsType = {
9696
/** The username from GitHub to fetch the repositories from. */
9797
username: string
9898
/** Debug flag - if true, the loader will output debug information. */
@@ -116,7 +116,7 @@ export const InternalLoaderOptions = LoaderOptions.required().extend({
116116
})
117117

118118
/** @internal */
119-
export type InternalLoaderOptions = Required<LoaderOptions> & {
119+
export type InternalLoaderOptionsType = Required<LoaderOptionsType> & {
120120
/** The last time the projects were updated. */
121121
lastUpdated: Date
122122
}

0 commit comments

Comments
 (0)