Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass domains to runtime options #333

Merged
merged 4 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ const imageModule: Module<ModuleOptions> = async function imageModule (moduleOpt
const imageOptions: Omit<CreateImageOptions, 'providers'> = pick(options, [
'screens',
'presets',
'provider'
'provider',
'domains'
])

options.static = options.static || {}
options.static.domains = options.domains

const providers = resolveProviders(nuxt, options)

// Run setup
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/providers/ipx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const operationsGenerator = createOperationsGenerator({
formatter: (key, val) => encodeQueryItem(key, val)
})

export const getImage: ProviderGetImage = (src, { modifiers = {}, baseURL = '/_ipx', domains = [] } = {}) => {
export const getImage: ProviderGetImage = (src, { modifiers = {}, baseURL = '/_ipx' } = {}, { options: { domains = [] } }) => {
if (modifiers.width && modifiers.height) {
modifiers.resize = `${modifiers.width}_${modifiers.height}`
delete modifiers.width
Expand Down
1 change: 1 addition & 0 deletions src/types/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface CreateImageOptions {
presets: { [name: string]: ImageOptions }
provider: string
screens?: Record<string, number>,
domains?: string[]
}

export interface ImageInfo {
Expand Down
26 changes: 14 additions & 12 deletions test/unit/providers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import * as netlify from '~/runtime/providers/netlify'
import * as prismic from '~/runtime/providers/prismic'
import * as sanity from '~/runtime/providers/sanity'

const emptyContext = { options: {} } as any

describe('Providers', () => {
test('ipx', () => {
const providerOptions = {}

for (const image of images) {
const [src, modifiers] = image.args
const generated = ipx.getImage(src, { modifiers: { ...modifiers }, ...providerOptions }, {} as any)
const generated = ipx.getImage(src, { modifiers: { ...modifiers }, ...providerOptions }, emptyContext)
generated.url = cleanDoubleSlashes(generated.url)
expect(generated).toMatchObject(image.ipx)
}
Expand All @@ -31,7 +33,7 @@ describe('Providers', () => {

for (const image of images) {
const [src, modifiers] = image.args
const generated = cloudinary.getImage(src, { modifiers, ...providerOptions }, {} as any)
const generated = cloudinary.getImage(src, { modifiers, ...providerOptions }, emptyContext)
expect(generated).toMatchObject(image.cloudinary)
}
})
Expand All @@ -50,7 +52,7 @@ describe('Providers', () => {
height: 300
},
...providerOptions
}, {} as any
}, emptyContext
)
expect(generated).toMatchObject({
url: `https://res.cloudinary.com/demo/image/fetch/f_auto,q_auto,w_300,h_300/${remoteUrl}`
Expand All @@ -69,7 +71,7 @@ describe('Providers', () => {
height: 300
},
...providerOptions
}, {} as any
}, emptyContext
)
expect(generated).toMatchObject({
url: 'https://res.cloudinary.com/demo/image/upload/f_auto,q_auto,w_300,h_300/remote/1/13/Benedict_Cumberbatch_2011.png'
Expand All @@ -83,7 +85,7 @@ describe('Providers', () => {

for (const image of images) {
const [src, modifiers] = image.args
const generated = twicpics.getImage(src, { modifiers, ...providerOptions }, {} as any)
const generated = twicpics.getImage(src, { modifiers, ...providerOptions }, emptyContext)
expect(generated).toMatchObject(image.twicpics)
}
})
Expand All @@ -94,7 +96,7 @@ describe('Providers', () => {
}
for (const image of images) {
const [src, modifiers] = image.args
const generated = glide.getImage(src, { modifiers, ...providerOptions }, {} as any)
const generated = glide.getImage(src, { modifiers, ...providerOptions }, emptyContext)
expect(generated).toMatchObject(image.glide)
}
})
Expand All @@ -105,7 +107,7 @@ describe('Providers', () => {
}
for (const image of images) {
const [src, modifiers] = image.args
const generated = fastly.getImage(src, { modifiers, ...providerOptions }, {} as any)
const generated = fastly.getImage(src, { modifiers, ...providerOptions }, emptyContext)
expect(generated).toMatchObject(image.fastly)
}
})
Expand All @@ -117,7 +119,7 @@ describe('Providers', () => {

for (const image of images) {
const [src, modifiers] = image.args
const generated = imgix.getImage(src, { modifiers, ...providerOptions }, {} as any)
const generated = imgix.getImage(src, { modifiers, ...providerOptions }, emptyContext)
expect(generated).toMatchObject(image.imgix)
}
})
Expand All @@ -129,7 +131,7 @@ describe('Providers', () => {

for (const image of images) {
const [src, modifiers] = image.args
const generated = imagekit.getImage(src, { modifiers, ...providerOptions }, {} as any)
const generated = imagekit.getImage(src, { modifiers, ...providerOptions }, emptyContext)
expect(generated).toMatchObject(image.imagekit)
}
})
Expand All @@ -141,7 +143,7 @@ describe('Providers', () => {

for (const image of images) {
const [src, modifiers] = image.args
const generated = netlify.getImage(src, { modifiers: { ...modifiers }, ...providerOptions }, {} as any)
const generated = netlify.getImage(src, { modifiers: { ...modifiers }, ...providerOptions }, emptyContext)
expect(generated).toMatchObject(image.netlify)
}
})
Expand All @@ -156,7 +158,7 @@ describe('Providers', () => {

for (const image of images) {
const [src, modifiers] = image.args
const generated = prismic.getImage(`${src}${EXISTING_QUERY_PARAMETERS}`, { modifiers, ...providerOptions }, {} as any)
const generated = prismic.getImage(`${src}${EXISTING_QUERY_PARAMETERS}`, { modifiers, ...providerOptions }, emptyContext)
expect(generated).toMatchObject(image.prismic)
}
})
Expand All @@ -169,7 +171,7 @@ describe('Providers', () => {

for (const image of images) {
const [, modifiers] = image.args
const generated = sanity.getImage('image-test-300x450-png', { modifiers: { ...modifiers }, ...providerOptions }, {} as any)
const generated = sanity.getImage('image-test-300x450-png', { modifiers: { ...modifiers }, ...providerOptions }, emptyContext)
expect(generated).toMatchObject(image.sanity)
}
})
Expand Down