Skip to content

Commit

Permalink
feat: add directus provider (nuxt#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 authored Jun 6, 2023
1 parent c0f3b00 commit 25c1426
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 6 deletions.
62 changes: 62 additions & 0 deletions docs/content/5.providers/directus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Directus

Nuxt Image with Directus integration

Integration between [Directus](https://directus.io/) and the image module.

To use this provider you just need to specify the base URL of your project.

```ts [nuxt.config.ts]
export default defineNuxtConfig({
image: {
directus: {
// This URL needs to include the final `assets/` directory
baseURL: 'http://localhost:8055/assets/',
}
}
})
```

You can easily override default options:

```ts [nuxt.config.ts]
export default defineNuxtConfig({
image: {
directus: {
baseURL: 'http://mydirectus-domain.com/assets/',
modifiers: {
withoutEnlargement: 'true',
transforms: [['blur', 4], ['negate']]
}
}
}
})
```

## Modifiers
All the default modifiers from [Directus documentation](https://docs.directus.io/reference/files.html#requesting-a-thumbnail) are available.

```vue
<nuxt-img
provider="directus"
src="ad514db1-eb90-4523-8183-46781437e7ee"
height="512"
fit="inside"
quality="20"
:modifiers="{ withoutEnlargement: 'true' }"
/>
```

Since Directus exposes [the full sharp API](https://sharp.pixelplumbing.com/api-operation) through the `transforms` parameter, we can use it inside our `modifiers` prop:

```vue
<nuxt-img
provider="directus"
src="ad514db1-eb90-4523-8183-46781437e7ee"
:modifiers="{ height: '512', withoutEnlargement: 'true', transforms: [['blur', 4], ['negate']] }"
/>
```

::alert{type="info"}
Note that the `transforms` parameter, as stated in the [Directus documentation](https://docs.directus.io/reference/files.html#advanced-transformations), is basically a list of lists. This is to facilitate the use of those sharp functions, like [`normalise`](https://sharp.pixelplumbing.com/api-operation#normalise), that would need multiple values in input: `transforms: [['normalise', 1, 99], ['blur', 4], ['negate']]`.
::
3 changes: 3 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export default defineNuxtConfig({
token: 'demo',
baseURL: 'sample.li'
},
directus: {
baseURL: 'http://localhost:8055/assets/'
},
fastly: {
baseURL: 'https://www.fastly.io'
},
Expand Down
31 changes: 31 additions & 0 deletions playground/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,5 +728,36 @@ export const providers: Provider[] = [
quality: 75
}
]
},
// Directus
{
name: 'directus',
samples: [
{
src: 'ad514db1-eb90-4523-8183-46781437e7ee',
alt: 'Image 1'
},
{
src: 'ad514db1-eb90-4523-8183-46781437e7ee.jpg',
alt: '1024px width',
width: 1024,
height: 256,
fit: 'cover',
modifiers: { withoutEnlargement: 'true' }
},
{
src: 'ad514db1-eb90-4523-8183-46781437e7ee',
alt: '256px width, webp',
width: 256,
format: 'webp'
},
{
src: 'ad514db1-eb90-4523-8183-46781437e7ee',
alt: '256px width, webp',
width: 256,
format: 'webp',
modifiers: { withoutEnlargement: 'true', transforms: [['blur', 4], ['negate']] }
}
]
}
]
1 change: 1 addition & 0 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const BuiltInProviders = [
'cloudimage',
'cloudinary',
'contentful',
'directus',
'edgio',
'fastly',
'glide',
Expand Down
20 changes: 20 additions & 0 deletions src/runtime/providers/directus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { joinURL } from 'ufo'
import type { ProviderGetImage } from '../../types'
import { createOperationsGenerator } from '#image'

export const operationsGenerator = createOperationsGenerator({
joinWith: '&'
})

export const getImage: ProviderGetImage = (src, { modifiers = {}, baseURL } = {}) => {
// Separating the transforms from the rest of the modifiers
const transforms = modifiers.transforms
if (transforms && transforms.length > 0) {
// We stringify and encode in URL the list of lists, then apply it back to the modifiers
modifiers.transforms = new URLSearchParams(JSON.stringify(transforms)).toString().replace(/=+$/, '') as unknown as (string | number)[][]
}
const operations = operationsGenerator(modifiers)
return {
url: joinURL(baseURL, src + (operations ? ('?' + operations) : ''))
}
}
18 changes: 18 additions & 0 deletions test/e2e/__snapshots__/no-ssr.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ exports[`browser (ssr: false) > contentful should render images 2`] = `
]
`;

exports[`browser (ssr: false) > directus should render images 1`] = `
[
"http://localhost:8055/assets/ad514db1-eb90-4523-8183-46781437e7ee",
"http://localhost:8055/assets/ad514db1-eb90-4523-8183-46781437e7ee.jpg?withoutEnlargement=true&width=1024&height=256&fit=cover",
"http://localhost:8055/assets/ad514db1-eb90-4523-8183-46781437e7ee?width=256&format=webp",
"http://localhost:8055/assets/ad514db1-eb90-4523-8183-46781437e7ee?withoutEnlargement=true&transforms=%5B%5B%22blur%22%2C4%5D%2C%5B%22negate%22%5D%5D&width=256&format=webp",
]
`;

exports[`browser (ssr: false) > directus should render images 2`] = `
[
"http://localhost:8055/assets/ad514db1-eb90-4523-8183-46781437e7ee",
"http://localhost:8055/assets/ad514db1-eb90-4523-8183-46781437e7ee.jpg?withoutEnlargement=true&width=1024&height=256&fit=cover",
"http://localhost:8055/assets/ad514db1-eb90-4523-8183-46781437e7ee?width=256&format=webp",
"http://localhost:8055/assets/ad514db1-eb90-4523-8183-46781437e7ee?withoutEnlargement=true&transforms=%5B%5B%22blur%22%2C4%5D%2C%5B%22negate%22%5D%5D&width=256&format=webp",
]
`;

exports[`browser (ssr: false) > edgio should render images 1`] = `
[
"https://opt.moovweb.net/?img=https://i.imgur.com/LFtQeX2.jpeg&height=200",
Expand Down
18 changes: 18 additions & 0 deletions test/e2e/__snapshots__/ssr.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ exports[`browser (ssr: true) > contentful should render images 2`] = `
]
`;

exports[`browser (ssr: true) > directus should render images 1`] = `
[
"http://localhost:8055/assets/ad514db1-eb90-4523-8183-46781437e7ee",
"http://localhost:8055/assets/ad514db1-eb90-4523-8183-46781437e7ee.jpg?withoutEnlargement=true&width=1024&height=256&fit=cover",
"http://localhost:8055/assets/ad514db1-eb90-4523-8183-46781437e7ee?width=256&format=webp",
"http://localhost:8055/assets/ad514db1-eb90-4523-8183-46781437e7ee?withoutEnlargement=true&transforms=%5B%5B%22blur%22%2C4%5D%2C%5B%22negate%22%5D%5D&width=256&format=webp",
]
`;

exports[`browser (ssr: true) > directus should render images 2`] = `
[
"http://localhost:8055/assets/ad514db1-eb90-4523-8183-46781437e7ee",
"http://localhost:8055/assets/ad514db1-eb90-4523-8183-46781437e7ee.jpg?withoutEnlargement=true&width=1024&height=256&fit=cover",
"http://localhost:8055/assets/ad514db1-eb90-4523-8183-46781437e7ee?width=256&format=webp",
"http://localhost:8055/assets/ad514db1-eb90-4523-8183-46781437e7ee?withoutEnlargement=true&transforms=%5B%5B%22blur%22%2C4%5D%2C%5B%22negate%22%5D%5D&width=256&format=webp",
]
`;

exports[`browser (ssr: true) > edgio should render images 1`] = `
[
"https://opt.moovweb.net/?img=https://i.imgur.com/LFtQeX2.jpeg&height=200",
Expand Down
18 changes: 12 additions & 6 deletions test/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const images = [
cloudimage: { url: 'https://demo.cloudimg.io/v7/test.png' },
edgio: { url: 'https://opt.moovweb.net/?img=/test.png' },
storyblok: { url: 'https://a.storyblok.com/test.png' },
vercel: { url: '/_vercel/image?url=/test.png&w=1536&q=100' }
vercel: { url: '/_vercel/image?url=/test.png&w=1536&q=100' },
directus: { url: '/assets/1ac73658-8b62-4dea-b6da-529fbc9d01a4' }
},
{
args: ['/test.png', { width: 200 }],
Expand All @@ -43,7 +44,8 @@ export const images = [
cloudimage: { url: 'https://demo.cloudimg.io/v7/test.png?width=200' },
edgio: { url: 'https://opt.moovweb.net/?img=/test.png&width=200' },
storyblok: { url: 'https://a.storyblok.com/test.png/m/200x0' },
vercel: { url: '/_vercel/image?url=/test.png&w=320&q=100' }
vercel: { url: '/_vercel/image?url=/test.png&w=320&q=100' },
directus: { url: '/assets/1ac73658-8b62-4dea-b6da-529fbc9d01a4?width=200' }
},
{
args: ['/test.png', { height: 200 }],
Expand All @@ -66,7 +68,8 @@ export const images = [
cloudimage: { url: 'https://demo.cloudimg.io/v7/test.png?height=200' },
edgio: { url: 'https://opt.moovweb.net/?img=/test.png&height=200' },
storyblok: { url: 'https://a.storyblok.com/test.png/m/0x200' },
vercel: { url: '/_vercel/image?url=/test.png&w=1536&q=100' }
vercel: { url: '/_vercel/image?url=/test.png&w=1536&q=100' },
directus: { url: '/assets/1ac73658-8b62-4dea-b6da-529fbc9d01a4?height=200' }
},
{
args: ['/test.png', { width: 200, height: 200 }],
Expand All @@ -89,7 +92,8 @@ export const images = [
cloudimage: { url: 'https://demo.cloudimg.io/v7/test.png?width=200&height=200' },
edgio: { url: 'https://opt.moovweb.net/?img=/test.png&width=200&height=200' },
storyblok: { url: 'https://a.storyblok.com/test.png/m/200x200' },
vercel: { url: '/_vercel/image?url=/test.png&w=320&q=100' }
vercel: { url: '/_vercel/image?url=/test.png&w=320&q=100' },
directus: { url: '/assets/1ac73658-8b62-4dea-b6da-529fbc9d01a4?width=200&height=200' }
},
{
args: ['/test.png', { width: 200, height: 200, fit: 'contain' }],
Expand All @@ -112,7 +116,8 @@ export const images = [
cloudimage: { url: 'https://demo.cloudimg.io/v7/test.png?width=200&height=200&func=fit' },
edgio: { url: 'https://opt.moovweb.net/?img=/test.png&width=200&height=200&fit=contain' },
storyblok: { url: 'https://a.storyblok.com/test.png/m/fit-contain/200x200' },
vercel: { url: '/_vercel/image?url=/test.png&w=320&q=100' }
vercel: { url: '/_vercel/image?url=/test.png&w=320&q=100' },
directus: { url: '/assets/1ac73658-8b62-4dea-b6da-529fbc9d01a4?width=200&height=200&fit=contain' }
},
{
args: ['/test.png', { width: 200, height: 200, fit: 'contain', format: 'jpeg' }],
Expand All @@ -135,7 +140,8 @@ export const images = [
cloudimage: { url: 'https://demo.cloudimg.io/v7/test.png?width=200&height=200&func=fit&force_format=jpeg' },
edgio: { url: 'https://opt.moovweb.net/?img=/test.png&width=200&height=200&fit=contain&format=jpeg' },
storyblok: { url: 'https://a.storyblok.com/test.png/m/fit-contain/200x200/filters:format(jpeg)' },
vercel: { url: '/_vercel/image?url=/test.png&w=320&q=100' }
vercel: { url: '/_vercel/image?url=/test.png&w=320&q=100' },
directus: { url: '/assets/1ac73658-8b62-4dea-b6da-529fbc9d01a4?width=200&height=200&fit=contain&format=jpg' }
}
] as const

Expand Down

0 comments on commit 25c1426

Please sign in to comment.