Skip to content

Commit

Permalink
refactor: use null to represent absence of value
Browse files Browse the repository at this point in the history
  • Loading branch information
futrime committed Nov 8, 2024
1 parent 53c5e39 commit 0dc921d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion apps/api/database-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { Package } from './package.js'
export interface DatabaseClient {
connect: () => Promise<void>
disconnect: () => Promise<void>
fetch: (source: string, identifier: string) => Promise<Package | undefined>
fetch: (source: string, identifier: string) => Promise<Package | null>
search: (q: string, perPage: number, page: number, sort: 'hotness' | 'updated', order: 'asc' | 'desc') => Promise<{ packages: Package[], pageCount: number }>
}
8 changes: 4 additions & 4 deletions apps/api/redis-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ export class RedisClient implements DatabaseClient {
await this.client.quit()
}

async fetch (identifier: string): Promise<Package | undefined> {
async fetch (identifier: string): Promise<Package | null> {
const entity = await this.repository.fetch(identifier)

// Check if the entity is an empty entity
if (entity.identifier === undefined) {
return undefined
// Check if the entity is empty
if (Object.keys(entity).length === 0) {
return null
}

return entity as Package
Expand Down
2 changes: 1 addition & 1 deletion apps/api/routes/packages/[identifier]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ router.get('/', (async (req, res, next) => {

try {
const packageData = await redisClient.fetch(identifier)
if (packageData === undefined) {
if (packageData === null) {
throw new createHttpError.NotFound('package not found')
}

Expand Down
10 changes: 5 additions & 5 deletions apps/bot/endstone-python-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class EndstonePythonFetcher extends GitHubFetcher {

for await (const repo of this.searchForRepositories(query)) {
try {
const packageInfo = await this.fetchPythonPackage(repo)
const packageInfo = await this.fetchPackage(repo)

yield packageInfo
} catch (error) {
Expand All @@ -21,7 +21,7 @@ export class EndstonePythonFetcher extends GitHubFetcher {
}
}

private async fetchPythonPackage (repo: RepositoryDescriptor): Promise<Package> {
private async fetchPackage (repo: RepositoryDescriptor): Promise<Package> {
consola.debug(`Fetching Endstone Python package github.com/${repo.owner}/${repo.repo}`)

const [repository, repositoryContributors, repositoryVersions, pyprojectMetadata] = await Promise.all([
Expand All @@ -45,7 +45,7 @@ export class EndstonePythonFetcher extends GitHubFetcher {
packageManager: 'pip'
}))

if (pypiPackageMetadata !== undefined) {
if (pypiPackageMetadata !== null) {
const pypiVersionStrings = Object.keys(pypiPackageMetadata.releases).filter(version => pypiPackageMetadata.releases[version].length > 0)

const pypiVersions = pypiVersionStrings.map(version => ({
Expand Down Expand Up @@ -92,11 +92,11 @@ export class EndstonePythonFetcher extends GitHubFetcher {
return toml.parse(data) as PythonProjectMetadata
}

private async fetchPypiPackageMetadata (name: string): Promise<PypiPackageMetadata | undefined> {
private async fetchPypiPackageMetadata (name: string): Promise<PypiPackageMetadata | null> {
const url = `https://pypi.org/pypi/${name}/json`
const response = await fetch(url)
if (!response.ok) {
return undefined
return null
}

return await response.json() as PypiPackageMetadata
Expand Down

0 comments on commit 0dc921d

Please sign in to comment.