Skip to content

Commit

Permalink
feat: add release-specific methods (wip) [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed May 26, 2021
1 parent b740406 commit 4f66405
Showing 1 changed file with 72 additions and 1 deletion.
73 changes: 72 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export class Client {
this.autoPreviewsEnabled = false
}

query = this.get
query = this.get.bind(this)

/**
* Queries content from the Prismic repository.
Expand Down Expand Up @@ -643,39 +643,70 @@ export class Client {
* Refs are used to identify which version of the repository's content should be queried. All repositories will have at least one ref pointing to the latest published content called the "master ref".
*
* @returns A list of all refs for the Prismic repository.
*
* @deprecated
*/
async getRefs(): Promise<Ref[]> {
const res = await this.fetch<Repository>(this.endpoint)

return res.refs
}

/**
* Returns a list of all Releases for the Prismic repository. Releases are used to group content changes before publishing.
*
* @returns A list of all Releases for the Prismic repository.
*/
getReleases = this.getRefs.bind(this)

/**
* Returns a ref for the Prismic repository with a matching ID.
*
* @param id ID of the ref.
*
* @returns The ref with a matching ID, if it exists.
*
* @deprecated
*/
async getRefById(id: string): Promise<Ref> {
const refs = await this.getRefs()

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

/**
* Returns a Release for the Prismic repository with a matching ID.
*
* @param id ID of the Release.
*
* @returns The Release with a matching ID, if it exists.
*/
getReleaseById = this.getRefById.bind(this)

/**
* Returns a ref for the Prismic repository with a matching label.
*
* @param label Label of the ref.
*
* @returns The ref with a matching label, if it exists.
*
* @deprecated
*/
async getRefByLabel(label: string): Promise<Ref> {
const refs = await this.getRefs()

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

/**
* Returns a Release for the Prismic repository with a matching label.
*
* @param label Label of the ref.
*
* @returns The ref with a matching label, if it exists.
*/
getReleaseByLabel = this.getRefByLabel.bind(this)

/**
* Returns the master ref for the Prismic repository. The master ref points to the repository's latest published content.
*
Expand Down Expand Up @@ -764,6 +795,43 @@ export class Client {
}
}

/**
* Sets the client to query content from currently published documents for all future queries.
*
* If the `ref` parameter is provided during a query, it takes priority for that query.
*/
async queryCurrentContent(): Promise<void> {
const masterRef = await this.getMasterRef()

this.ref = masterRef.ref
}

/**
* Sets the client to query content from a specific Release identified by its ID for all future queries.
*
* If the `ref` parameter is provided during a query, it takes priority for that query.
*
* @param releaseId The ID of the Release.
*/
async queryContentFromReleaseByID(releaseId: string): Promise<void> {
const releaseRef = await this.getRefById(releaseId)

this.ref = releaseRef.ref
}

/**
* Sets the client to query content from a specific Release identified by its label for all future queries.
*
* If the `ref` parameter is provided during a query, it takes priority for that query.
*
* @param releaseLabel The label of the Release.
*/
async queryContentFromReleaseByLabel(releaseLabel: string): Promise<void> {
const releaseRef = await this.getRefByLabel(releaseLabel)

this.ref = releaseRef.ref
}

/**
* Returns the preview ref for the client, if one exists.
*
Expand Down Expand Up @@ -813,6 +881,9 @@ export class Client {

const masterRef = await this.getMasterRef()

// We will persist the master ref to avoid refetching repository metadata.
this.ref = masterRef.ref

return masterRef.ref
}

Expand Down

0 comments on commit 4f66405

Please sign in to comment.