Skip to content

Commit

Permalink
refactor: use @prismicio/types where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed May 22, 2021
1 parent ec26d6a commit bb2b514
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 52 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"license": "Apache-2.0",
"author": "Prismic <contact@prismic.io> (https://prismic.io)",
"type": "module",
"exports": {
".": {
"require": "./dist/index.js",
Expand Down Expand Up @@ -61,6 +62,7 @@
"devDependencies": {
"@commitlint/cli": "^12.1.4",
"@commitlint/config-conventional": "^12.1.4",
"@prismicio/types": "^0.0.6",
"@types/node-fetch": "^2.5.10",
"@types/sinon": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^4.24.0",
Expand Down
12 changes: 2 additions & 10 deletions src/buildQueryURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ValueOf } from 'type-fest'

import { castArray } from './lib/castArray'

import { Ordering } from './types'

/**
* Parameters for the Prismic REST API V2.
*
Expand Down Expand Up @@ -91,16 +93,6 @@ type BuildQueryURLParams = {
predicates?: string | string[]
}

/**
* An `orderings` parameter that orders the results by the specified field.
*
* {@link https://prismic.io/docs/technologies/search-parameters-reference-rest-api#orderings}
*/
type Ordering = {
field: string
direction?: 'asc' | 'desc'
}

/**
* Parameters in this map have been renamed from the official Prismic REST API
* V2 specification for better developer ergonomics.
Expand Down
31 changes: 16 additions & 15 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as prismicT from '@prismicio/types'

import { appendPredicates } from './lib/appendPredicates'
import { getCookie } from './lib/getCookie'

import {
Document,
FetchLike,
HttpRequestLike,
LinkResolver,
Expand Down Expand Up @@ -277,7 +278,7 @@ export class Client {
* const response = await client.get()
* ```
*/
async get<TDocument extends Document>(
async get<TDocument extends prismicT.PrismicDocument>(
params?: Partial<BuildQueryURLArgs>,
): Promise<Query<TDocument>> {
const url = await this.buildQueryURL(params)
Expand All @@ -298,7 +299,7 @@ export class Client {
* const document = await client.getFirst()
* ```
*/
async getFirst<TDocument extends Document>(
async getFirst<TDocument extends prismicT.PrismicDocument>(
params?: Partial<BuildQueryURLArgs>,
): Promise<TDocument> {
const result = await this.get<TDocument>(params)
Expand Down Expand Up @@ -326,7 +327,7 @@ export class Client {
* const response = await client.getAll()
* ```
*/
async getAll<TDocument extends Document>(
async getAll<TDocument extends prismicT.PrismicDocument>(
params: Partial<Omit<BuildQueryURLArgs, 'page'>> & GetAllParams = {},
): Promise<TDocument[]> {
const { limit = Infinity, ...actualParams } = params
Expand Down Expand Up @@ -364,7 +365,7 @@ export class Client {
* const document = await client.getByID('WW4bKScAAMAqmluX')
* ```
*/
async getByID<TDocument extends Document>(
async getByID<TDocument extends prismicT.PrismicDocument>(
id: string,
params?: Partial<BuildQueryURLArgs>,
): Promise<TDocument> {
Expand All @@ -391,7 +392,7 @@ export class Client {
* const response = await client.getByIDs(['WW4bKScAAMAqmluX', 'U1kTRgEAAC8A5ldS'])
* ```
*/
async getByIDs<TDocument extends Document>(
async getByIDs<TDocument extends prismicT.PrismicDocument>(
ids: string[],
params?: Partial<BuildQueryURLArgs>,
): Promise<Query<TDocument>> {
Expand Down Expand Up @@ -420,7 +421,7 @@ export class Client {
* const response = await client.getAllByIDs(['WW4bKScAAMAqmluX', 'U1kTRgEAAC8A5ldS'])
* ```
*/
async getAllByIDs<TDocument extends Document>(
async getAllByIDs<TDocument extends prismicT.PrismicDocument>(
ids: string[],
params?: Partial<BuildQueryURLArgs>,
): Promise<TDocument[]> {
Expand Down Expand Up @@ -448,7 +449,7 @@ export class Client {
* const document = await client.getByUID('blog_post', 'my-first-post')
* ```
*/
async getByUID<TDocument extends Document>(
async getByUID<TDocument extends prismicT.PrismicDocument>(
documentType: string,
uid: string,
params?: Partial<BuildQueryURLArgs>,
Expand Down Expand Up @@ -479,7 +480,7 @@ export class Client {
* const document = await client.getSingle('settings')
* ```
*/
async getSingle<TDocument extends Document>(
async getSingle<TDocument extends prismicT.PrismicDocument>(
documentType: string,
params?: Partial<BuildQueryURLArgs>,
): Promise<TDocument> {
Expand All @@ -504,7 +505,7 @@ export class Client {
* const response = await client.getByType('blog_post')
* ```
*/
async getByType<TDocument extends Document>(
async getByType<TDocument extends prismicT.PrismicDocument>(
documentType: string,
params?: Partial<BuildQueryURLArgs>,
): Promise<Query<TDocument>> {
Expand All @@ -529,7 +530,7 @@ export class Client {
* const response = await client.getByType('blog_post')
* ```
*/
async getAllByType<TDocument extends Document>(
async getAllByType<TDocument extends prismicT.PrismicDocument>(
documentType: string,
params?: Partial<Omit<BuildQueryURLArgs, 'page'>>,
): Promise<TDocument[]> {
Expand All @@ -554,7 +555,7 @@ export class Client {
* const response = await client.getByTag('food')
* ```
*/
async getByTag<TDocument extends Document>(
async getByTag<TDocument extends prismicT.PrismicDocument>(
tag: string,
params?: Partial<BuildQueryURLArgs>,
): Promise<Query<TDocument>> {
Expand All @@ -579,7 +580,7 @@ export class Client {
* const response = await client.getAllByTag('food')
* ```
*/
async getAllByTag<TDocument extends Document>(
async getAllByTag<TDocument extends prismicT.PrismicDocument>(
tag: string,
params?: Partial<Omit<BuildQueryURLArgs, 'page'>>,
): Promise<TDocument[]> {
Expand All @@ -602,7 +603,7 @@ export class Client {
* const response = await client.getAllByTag('food')
* ```
*/
async getByTags<TDocument extends Document>(
async getByTags<TDocument extends prismicT.PrismicDocument>(
tags: string[],
params?: Partial<BuildQueryURLArgs>,
): Promise<Query<TDocument>> {
Expand All @@ -627,7 +628,7 @@ export class Client {
* const response = await client.getAllByTag('food')
* ```
*/
async getAllByTags<TDocument extends Document>(
async getAllByTags<TDocument extends prismicT.PrismicDocument>(
tags: string[],
params?: Partial<Omit<BuildQueryURLArgs, 'page'>>,
): Promise<TDocument[]> {
Expand Down
44 changes: 17 additions & 27 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TODO: Migrate types to @prismicio/types and use that library throughout the package.
import * as prismicT from '@prismicio/types'

export interface Ref {
ref: string
Expand All @@ -13,28 +13,6 @@ export interface Language {
name: string
}

export interface Document<Data = Record<string, unknown>> {
id: string
uid?: string
url?: string
type: string
href: string
tags: string[]
slugs: string[]
lang?: string
alternate_languages: AlternateLanguage[]
first_publication_date: string | null
last_publication_date: string | null
data: Data
}

export interface AlternateLanguage {
id: string
uid?: string
type: string
lang: string
}

export interface Repository {
refs: Ref[]
bookmarks: Record<string, string>
Expand All @@ -52,7 +30,9 @@ export interface Repository {
license: string
}

export interface Query<TDocument extends Document = Document> {
export interface Query<
TDocument extends prismicT.PrismicDocument = prismicT.PrismicDocument,
> {
page: number
results_per_page: number
results_size: number
Expand All @@ -78,9 +58,9 @@ export interface FormField {
default?: string
}

export type LinkResolver<TDocument extends Document = Document> = (
document: TDocument,
) => string
export type LinkResolver<
TDocument extends prismicT.PrismicDocument = prismicT.PrismicDocument,
> = (document: TDocument) => string

/**
* A universal API to make network requests. A subset of the `fetch()` API.
Expand Down Expand Up @@ -115,3 +95,13 @@ export interface HttpRequestLike {
}
query?: Record<string, unknown>
}

/**
* An `orderings` parameter that orders the results by the specified field.
*
* {@link https://prismic.io/docs/technologies/search-parameters-reference-rest-api#orderings}
*/
export interface Ordering {
field: string
direction?: 'asc' | 'desc'
}

0 comments on commit bb2b514

Please sign in to comment.