Skip to content

Commit

Permalink
feat: support types for config
Browse files Browse the repository at this point in the history
also remove runtime* prefix from types
  • Loading branch information
pi0 committed Dec 23, 2020
1 parent 69038ec commit 4d7bf06
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 70 deletions.
10 changes: 8 additions & 2 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export default {
import type { NuxtConfig } from '@nuxt/types'
import '../src/types'

export default <NuxtConfig> {
components: true,
target: 'server',
modules: [
Expand All @@ -8,7 +11,10 @@ export default {
'@nuxt/typescript-build'
],
image: {
accept: ['nuxtjs.org', 'unsplash.com'],
accept: [
'nuxtjs.org',
'unsplash.com'
],
twicpics: {
baseURL: 'https://nuxt-demo.twic.pics'
},
Expand Down
5 changes: 3 additions & 2 deletions playground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
},
"types": [
"@types/node",
"@nuxt/types"
"@nuxt/types",
"../src/types"
]
},
"exclude": [
"node_modules"
]
}
}
19 changes: 14 additions & 5 deletions src/provider.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { normalize } from 'upath'
import type { ModuleOptions, ImageProvider, ProviderFactory, InputProvider } from './types'
import type { ModuleOptions, ProviderFactory, InputProvider } from './types'
import { hash } from './utils'
import * as PROVIDERS from './providers'

export function resolveProviders (nuxt, options: ModuleOptions): ImageProvider[] {
const providers: ImageProvider[] = []
export interface ImageModuleProvider {
name: string
importName: string
options: any
provider: ProviderFactory
runtime: string
runtimeOptions: any
}

export function resolveProviders (nuxt, options: ModuleOptions): ImageModuleProvider[] {
const providers: ImageModuleProvider[] = []

for (const key in options) {
if (PROVIDERS[key]) {
Expand All @@ -19,7 +28,7 @@ export function resolveProviders (nuxt, options: ModuleOptions): ImageProvider[]
return providers
}

export function resolveProvider (nuxt: any, key: string, input: InputProvider): ImageProvider {
export function resolveProvider (nuxt: any, key: string, input: InputProvider): ImageModuleProvider {
if (!input.name) {
input.name = key
}
Expand All @@ -40,7 +49,7 @@ export function resolveProvider (nuxt: any, key: string, input: InputProvider):

// TODO: Check existence of runtime

return <ImageProvider> {
return <ImageModuleProvider> {
...input,
runtime: normalize(runtime),
importName: `${key}Runtime$${hash(runtime, 4)}`,
Expand Down
4 changes: 2 additions & 2 deletions src/providers/cloudinary/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuntimeProviderGetImage } from 'src'
import type { ProviderGetImage } from 'src'
import { createOperationsGenerator } from '@nuxt/image/runtime'

const convertHextoRGBFormat = (value: string) => value.startsWith('#') ? value.replace('#', 'rgb_') : value
Expand Down Expand Up @@ -74,7 +74,7 @@ const defaultModifiers = {
quality: 'auto'
}

export const getImage: RuntimeProviderGetImage = (src, { modifiers, baseURL }) => {
export const getImage: ProviderGetImage = (src, { modifiers, baseURL }) => {
const mergeModifiers = { ...defaultModifiers, ...modifiers }

const srcWithoutExtension = src.replace(/\.[^/.]+$/, '')
Expand Down
4 changes: 2 additions & 2 deletions src/providers/fastly/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuntimeProviderGetImage } from 'src'
import type { ProviderGetImage } from 'src'
import { createOperationsGenerator } from '@nuxt/image/runtime'

const operationsGenerator = createOperationsGenerator({
Expand All @@ -15,7 +15,7 @@ const operationsGenerator = createOperationsGenerator({
formatter: (key, value) => `${key}=${value}`
})

export const getImage: RuntimeProviderGetImage = (src, { modifiers, baseURL }) => {
export const getImage: ProviderGetImage = (src, { modifiers, baseURL }) => {
const operations = operationsGenerator(modifiers)
return {
url: baseURL + src + '?' + operations
Expand Down
4 changes: 2 additions & 2 deletions src/providers/imagekit/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuntimeProviderGetImage } from 'src'
import type { ProviderGetImage } from 'src'
import { createOperationsGenerator } from '@nuxt/image/runtime'

const operationsGenerator = createOperationsGenerator({
Expand Down Expand Up @@ -29,7 +29,7 @@ const operationsGenerator = createOperationsGenerator({
formatter: (key, value) => `${key}-${value}`
})

export const getImage: RuntimeProviderGetImage = (src, { modifiers, baseURL }) => {
export const getImage: ProviderGetImage = (src, { modifiers, baseURL }) => {
let operations = operationsGenerator(modifiers)
operations = operations.replace('c-pad_resize', 'cm-pad_resize')
return {
Expand Down
4 changes: 2 additions & 2 deletions src/providers/imgix/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuntimeProviderGetImage } from 'src'
import type { ProviderGetImage } from 'src'
import { createOperationsGenerator } from '@nuxt/image/runtime'

const operationsGenerator = createOperationsGenerator({
Expand All @@ -20,7 +20,7 @@ const operationsGenerator = createOperationsGenerator({
formatter: (key, value) => `${key}=${value}`
})

export const getImage: RuntimeProviderGetImage = (src, { modifiers, baseURL }) => {
export const getImage: ProviderGetImage = (src, { modifiers, baseURL }) => {
const operations = operationsGenerator(modifiers)
return {
url: baseURL + src + '?' + operations
Expand Down
4 changes: 2 additions & 2 deletions src/providers/local/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RuntimeProviderGetImage } from 'src'
import { ProviderGetImage } from 'src'
import { hasProtocol, joinURL } from 'ufo'
import { createOperationsGenerator } from '@nuxt/image/runtime'

Expand All @@ -23,7 +23,7 @@ const operationsGenerator = createOperationsGenerator({
formatter: (key, value) => `${key}_${value}`
})

export const getImage: RuntimeProviderGetImage = (src, { modifiers, baseURL }) => {
export const getImage: ProviderGetImage = (src, { modifiers, baseURL }) => {
const format = modifiers.format || '_'
delete modifiers.format

Expand Down
4 changes: 2 additions & 2 deletions src/providers/twicpics/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuntimeProviderGetImage } from 'src'
import type { ProviderGetImage } from 'src'
import { createMapper, createOperationsGenerator } from '@nuxt/image/runtime'

const fits = createMapper({
Expand Down Expand Up @@ -28,7 +28,7 @@ const operationsGenerator = createOperationsGenerator({
formatter: (key, value) => `${key}=${value}`
})

export const getImage: RuntimeProviderGetImage = (src, { modifiers, baseURL }) => {
export const getImage: ProviderGetImage = (src, { modifiers, baseURL }) => {
const { width, height, fit, ...providerModifiers } = modifiers

if (width || height) {
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/meta.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { RuntimeImageInfo } from '../types/image'
import type { ImageInfo } from '../types/image'
import type { ImageCTX } from './image'

export async function imageMeta (ctx: ImageCTX, url): Promise<RuntimeImageInfo> {
export async function imageMeta (ctx: ImageCTX, url): Promise<ImageInfo> {
const cache = getCache(ctx)

const cacheKey = 'image:meta:' + url
Expand All @@ -23,7 +23,7 @@ export async function imageMeta (ctx: ImageCTX, url): Promise<RuntimeImageInfo>
return meta
}

async function _imageMeta (url): Promise<RuntimeImageInfo> {
async function _imageMeta (url): Promise<ImageInfo> {
if (process.client) {
if (typeof Image === 'undefined') {
throw new TypeError('Image not supported')
Expand Down
12 changes: 12 additions & 0 deletions src/types/global.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { $Image } from './image'
import { ModuleOptions } from './module'

declare module '@nuxt/types' {
interface Context {
Expand All @@ -8,10 +9,21 @@ declare module '@nuxt/types' {
interface NuxtAppOptions {
$img: $Image
}

interface Configuration {
image?: Partial<ModuleOptions>
}
}

declare module 'vue/types/vue' {
interface Vue {
$img: $Image
}
}

declare module 'vuex/types/index' {
// eslint-disable-next-line no-unused-vars,@typescript-eslint/no-unused-vars
interface Store<S> {
$img: $Image
}
}
31 changes: 19 additions & 12 deletions src/types/image.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AllowlistOptions, Matcher } from 'allowlist'
import type { AllowlistOptions } from 'allowlist'

export interface ImageModifiers {
width: number
Expand All @@ -20,11 +20,11 @@ export interface ImageOptions {
size?: String | Partial<ImageSize>[]
provider?: string,
preset?: string,
modifiers?: ImageModifiers
modifiers?: Partial<ImageModifiers>
[key: string]: any
}

export interface RuntimeImageInfo {
export interface ImageInfo {
width: number,
height: number,
placeholder?: string,
Expand All @@ -33,14 +33,21 @@ export interface RuntimeImageInfo {
export interface RuntimeImage {
url: string,
isStatic?: boolean,
getMeta?: () => Promise<RuntimeImageInfo>
getMeta?: () => Promise<ImageInfo>
}

export type ProviderGetImage = (src: string, options: ImageOptions) => RuntimeImage

export interface RuntimeProvider {
defaults?: any
getImage: ProviderGetImage
}

export interface CreateImageOptions {
providers: {
[name: string]: {
defaults: any,
provider: any
provider: RuntimeProvider
}
}
presets: { [name: string]: ImageOptions }
Expand All @@ -53,7 +60,7 @@ export interface CreateImageOptions {
export interface ResolvedImage {
input: string
image: RuntimeImage
provider: any
provider: RuntimeProvider
preset: ImageOptions
}

Expand All @@ -62,19 +69,19 @@ export interface $Image {
[preset: string]: (source: string) => any
}

export interface RuntimePlaceholder extends RuntimeImageInfo {
export interface RuntimePlaceholder extends ImageInfo {
url: string;
}

export type RuntimeOperationFormatter = (key: string, value: string) => string
export type OperationFormatter = (key: string, value: string) => string

export type RuntimeOperationMapper = { [key: string]: string | false } | ((key: string) => string)
export type OperationMapper = { [key: string]: string | false } | ((key: string) => string)

export interface OperationGeneratorConfig {
keyMap?: RuntimeOperationMapper
formatter?: RuntimeOperationFormatter
keyMap?: OperationMapper
formatter?: OperationFormatter
joinWith?: string
valueMap?: {
[key: string]: RuntimeOperationMapper
[key: string]: OperationMapper
}
}
1 change: 0 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ import './global'

export * from './image'
export * from './module'
export * from './provider'
24 changes: 22 additions & 2 deletions src/types/module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import type { ImageOptions } from './image'
import type { InputProvider } from './provider'

export interface Provider {
runtime: string
runtimeOptions: any
}

export type ProviderFactory = (options: any) => Provider

export interface InputProvider {
name?: string
provider?: string | ProviderFactory
options?: any
baseURL?: string
}

export interface ModuleOptions {
provider: string
presets: ImageOptions[]
presets: Partial<ImageOptions>[]
local: {
baseURL: string
dir: string
Expand All @@ -17,5 +30,12 @@ export interface ModuleOptions {
providers: { [name: string]: InputProvider }
accept: any
intersectOptions: object

cloudinary: InputProvider,
fastly: InputProvider,
imagekit: InputProvider,
imgix: InputProvider,
twicpics: InputProvider,

[provider: string]: InputProvider | any
}
30 changes: 0 additions & 30 deletions src/types/provider.ts

This file was deleted.

3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
],
"types": [
"@nuxt/types",
"@types/node"
"@types/node",
"./src/types"
],
"paths": {
"@nuxt/image/*": [ "src/*" ]
Expand Down

0 comments on commit 4d7bf06

Please sign in to comment.