Skip to content

Commit

Permalink
refactor: share code among tags and ref fns
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed May 5, 2021
1 parent f0c5206 commit 5c9e7ea
Showing 1 changed file with 28 additions and 39 deletions.
67 changes: 28 additions & 39 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ const firstResult = <TDocument extends Document>(
queryResponse: Query<TDocument>,
): TDocument => queryResponse.results[0]

const findRef = (refs: Ref[], predicate: (ref: Ref) => boolean): Ref => {
const ref = refs.find((ref) => predicate(ref))

if (!ref) {
throw new Error('Ref could not be found.')
}

return ref
}

export const createClient = (
...args: ConstructorParameters<typeof Client>
): Client => new Client(...args)
Expand Down Expand Up @@ -181,35 +191,27 @@ export class Client {
)
}

getByTags = this.getByTag
async getByTag<TDocument extends Document>(
tag: string,
tags: string[],
params?: Partial<BuildQueryURLArgs>,
): Promise<Query<TDocument>>
async getByTag<TDocument extends Document>(
tags: string | string[],
params?: Partial<BuildQueryURLArgs>,
): Promise<Query<TDocument>> {
return await this.get<TDocument>(
appendPredicates(tagsPredicate(tag))(params),
appendPredicates(tagsPredicate(tags))(params),
)
}

getAllByTags = this.getAllByTag
async getAllByTag<TDocument extends Document>(
tag: string,
params?: Partial<BuildQueryURLArgs>,
): Promise<TDocument[]> {
return await this.getAll<TDocument>(
appendPredicates(tagsPredicate(tag))(params),
)
}

async getByTags<TDocument extends Document>(
tags: string[],
params?: Partial<BuildQueryURLArgs>,
): Promise<Query<TDocument>> {
return await this.get<TDocument>(
appendPredicates(tagsPredicate(tags))(params),
)
}

async getAllByTags<TDocument extends Document>(
tags: string[],
): Promise<TDocument[]>
async getAllByTag<TDocument extends Document>(
tags: string | string[],
params?: Partial<BuildQueryURLArgs>,
): Promise<TDocument[]> {
return await this.getAll<TDocument>(
Expand All @@ -225,39 +227,26 @@ export class Client {

async getRefById(id: string): Promise<Ref> {
const refs = await this.getRefs()
const ref = refs.find((ref) => ref.id === id)

if (!ref) {
throw new Error('Ref could not be found.')
}

return ref
return findRef(refs, (ref) => ref.id === id)
}

async getRefByLabel(label: string): Promise<Ref> {
const refs = await this.getRefs()
const ref = refs.find((ref) => ref.label === label)

if (!ref) {
throw new Error('Ref could not be found.')
}

return ref
return findRef(refs, (ref) => ref.label === label)
}

async getMasterRef(): Promise<Ref> {
const refs = await this.getRefs()
const masterRef = refs.find((ref) => ref.isMasterRef)

if (!masterRef) {
throw new Error('Master ref could not be found.')
}

return masterRef
return findRef(refs, (ref) => ref.isMasterRef)
}

async buildQueryURL(params?: Partial<BuildQueryURLArgs>): Promise<string> {
const ref = params?.ref ?? (await this.getResolvedRefString())
async buildQueryURL(
params: Partial<BuildQueryURLArgs> = {},
): Promise<string> {
const ref = params.ref || (await this.getResolvedRefString())

return buildQueryURL(this.endpoint, {
...this.params,
Expand Down

0 comments on commit 5c9e7ea

Please sign in to comment.