Skip to content

Commit

Permalink
fix: add types for pick utility and fix issues with imageOptions (#156)
Browse files Browse the repository at this point in the history
* fix: add type for `pick()` utility

* fix imageOptions

Co-authored-by: Pooya Parsa <pyapar@gmail.com>
  • Loading branch information
danielroe and pi0 authored Jan 22, 2021
1 parent 9db5aa8 commit 63aaa8a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
13 changes: 10 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve } from 'path'
import defu from 'defu'
import type { ModuleOptions } from './types'
import type { ModuleOptions, CreateImageOptions } from './types'
import { pick, pkg } from './utils'
import { setupStaticGeneration } from './generate'
import { resolveProviders } from './provider'
Expand All @@ -11,7 +11,7 @@ async function imageModule (moduleOptions: ModuleOptions) {

const defaults: ModuleOptions = {
provider: 'static',
presets: [],
presets: {},
static: {
baseURL: '/_img',
dir: resolve(nuxt.options.srcDir, nuxt.options.dir.static),
Expand All @@ -35,7 +35,14 @@ async function imageModule (moduleOptions: ModuleOptions) {

options.provider = process.env.NUXT_IMAGE_PROVIDER || options.provider || 'static'

const imageOptions = pick(options, ['sizes', 'presets', 'provider', 'intersectOptions', 'accept'])
const imageOptions: Omit<CreateImageOptions, 'providers'> = pick(options, [
'sizes',
'presets',
'provider',
'intersectOptions',
'accept'
])

const providers = await resolveProviders(nuxt, options)

// Transpile and alias runtime
Expand Down
7 changes: 3 additions & 4 deletions src/types/image.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ export interface CreateImageOptions {
presets: { [name: string]: ImageOptions }
provider: string
intersectOptions: object
responsiveSizes: number[]
sizes: string[]
allow: AllowlistOptions
sizes?: (number | string)[]
accept: AllowlistOptions
}

export interface ImageCTX {
options: CreateImageOptions,
allow: Matcher<any>
accept: Matcher<any>
nuxtContext: {
ssrContext: any
cache?: any
Expand Down
4 changes: 2 additions & 2 deletions src/types/module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface ImageProviders {

export interface ModuleOptions extends ImageProviders {
provider: string
presets: Partial<ImageOptions>[]
presets: { [name: string]: ImageOptions }
static: {
baseURL: string
dir: string
Expand All @@ -26,7 +26,7 @@ export interface ModuleOptions extends ImageProviders {
accept: string[]
sharp: { [key: string]: any }
}
sizes?: number[],
sizes?: (number|string)[],
internalUrl?: string
accept: any
intersectOptions: object
Expand Down
5 changes: 2 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ export function hash (value: string, length = 6) {
return hasha(value).substr(0, length)
}

// TODO: Typechecks
export function pick (obj: any, keys: string[]) {
const newobj = {}
export function pick<O extends Record<any, any>, K extends keyof O> (obj: O, keys: K[]): Pick<O, K> {
const newobj = {} as Pick<O, K>
for (const key of keys) {
newobj[key] = obj[key]
}
Expand Down

0 comments on commit 63aaa8a

Please sign in to comment.