Skip to content

Commit

Permalink
refactor: client
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed May 5, 2021
1 parent 97bfb05 commit f0c5206
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 128 deletions.
34 changes: 10 additions & 24 deletions src/buildQueryURL.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IterableElement, ValueOf } from 'type-fest'
import { ValueOf } from 'type-fest'

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

Expand Down Expand Up @@ -111,23 +111,13 @@ const RENAMED_PARAMS = {
accessToken: 'access_token',
} as const

/**
* Parameter keys in this list are not actual Prismic REST API V2 parameters.
* They are used for other API inputs and functionality.
*
* These parameters are *not* included in URL builder products.
*
* This list should match parameters included in `BuildQueryURLParams`.
*/
const NON_PARAM_ARGS = ['ref', 'predicates'] as const

/**
* A valid parameter name for the Prismic REST API V2.
*/
type ValidParamName =
| Exclude<
keyof QueryParams,
keyof typeof RENAMED_PARAMS | IterableElement<typeof NON_PARAM_ARGS>
keyof typeof RENAMED_PARAMS | keyof BuildQueryURLParams
>
| ValueOf<typeof RENAMED_PARAMS>

Expand All @@ -142,7 +132,7 @@ type ValidParamName =
const castOrderingToString = (ordering: Ordering | string): string =>
typeof ordering === 'string'
? ordering
: [ordering.field, ordering.direction].join(' ')
: [ordering.field, ordering.direction].filter(Boolean).join(' ')

export type BuildQueryURLArgs = QueryParams & BuildQueryURLParams

Expand Down Expand Up @@ -186,19 +176,15 @@ export const buildQueryURL = (

let value: string | string[] | null | undefined

switch (name) {
case 'orderings': {
const scopedValue = params[name]

if (scopedValue) {
const v = castArray(scopedValue)
.map((ordering) => castOrderingToString(ordering))
.join(',')
if (name === 'orderings') {
const scopedValue = params[name]

value = `[${v}]`
}
if (scopedValue) {
const v = castArray(scopedValue)
.map((ordering) => castOrderingToString(ordering))
.join(',')

break
value = `[${v}]`
}
}

Expand Down
Loading

0 comments on commit f0c5206

Please sign in to comment.