Skip to content

Commit

Permalink
chore: add resolve resources function
Browse files Browse the repository at this point in the history
  • Loading branch information
nozomuikuta committed Jul 23, 2022
1 parent ac2d8f8 commit c3f37a4
Show file tree
Hide file tree
Showing 6 changed files with 670 additions and 34 deletions.
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,22 @@
"prepack": "nuxt-module-build",
"dev": "nuxi dev playground",
"dev:build": "nuxi build playground",
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground"
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
"test": "vitest",
"test:run": "vitest run",
"test:coverage": "vitest run --coverage",
"test:coverage:serve": "yarn run test:coverage && yarn run serve -p 5000 ./coverage"
},
"dependencies": {
"@nuxt/kit": "^3.0.0-rc.5"
"@nuxt/kit": "^3.0.0-rc.5",
"feed": "^4.2.2"
},
"devDependencies": {
"@nuxt/module-builder": "latest",
"@nuxtjs/eslint-config-typescript": "latest",
"c8": "^7.12.0",
"eslint": "latest",
"nuxt": "^3.0.0-rc.5"
"nuxt": "^3.0.0-rc.5",
"vitest": "^0.18.1"
}
}
22 changes: 21 additions & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,25 @@ export default defineNuxtConfig({
modules: [
FeedModule
],
feed: {}
feed: {
options: {
title: 'My blog',
description: 'This is my personal feed!'
},
create () {
console.log('common create function')
},
data: ['common data'],
sources: [
{
type: 'rss2',
path: '/feed.xml',
options: {},
create () {
console.log('source create function')
},
data: ['source data']
}
]
}
})
24 changes: 24 additions & 0 deletions src/feed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Feed, FeedOptions } from 'feed'
import type { ModuleOptions } from './module'

export type FeedSource = {
type: 'atom1' | 'json1' | 'rss2'
path: string
options?: FeedOptions
create?(feed: Feed, data: FeedSource['data'], commonData: FeedSource['data']): void | Promise<void>
data?: any[]
}

export type FeedSourcesFactory = () => FeedSource[] | Promise<FeedSource[]>

export async function resolveSources (sources: ModuleOptions['sources']): Promise<FeedSource[]> {
if (Array.isArray(sources)) {
return sources
}

if (typeof sources === 'function') {
return await sources()
}

throw new TypeError('`sources` option must be an array of feed sources or a function that returns it')
}
19 changes: 16 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { defineNuxtModule } from '@nuxt/kit'
import { resolveSources } from './feed'
import type { FeedSource, FeedSourcesFactory } from './feed'

export interface ModuleOptions {}
export interface ModuleOptions {
options?: Partial<FeedSource['options']>
create?: FeedSource['create']
data?: FeedSource['data']
sources: FeedSource[] | FeedSourcesFactory
}

export default defineNuxtModule<ModuleOptions>({
meta: {
Expand All @@ -10,6 +17,12 @@ export default defineNuxtModule<ModuleOptions>({
nuxt: '^3.0.0'
}
},
defaults: {},
setup (options, nuxt) {}
defaults: {
sources: []
},
async setup (options, nuxt) {
console.log(options)
const sources = await resolveSources(options.sources)
console.log(sources)
}
})
36 changes: 36 additions & 0 deletions test/feed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect, it, describe } from 'vitest'
import { resolveSources } from '../src/feed'
import type { FeedSource, FeedSourcesFactory } from '../src/feed'

const sources: FeedSource[] = [
{
type: 'atom1',
path: '/feed.atom'
},
{
type: 'json1',
path: '/feed.json'
},
{
type: 'rss2',
path: '/feed.xml'
}
]

const sourcesFactory: FeedSourcesFactory = () => sources

describe('feed', () => {
describe('resolveSources', () => {
it('should receive an array and is fulfilled with the received array', async () => {
expect(await resolveSources(sources)).toBe(sources)
})

it('should receive a function and is fulfilled with an array return by the function', async () => {
expect(await resolveSources(sourcesFactory)).toBe(sources)
})

it('should throw a type error if neither an array nor a function is passed', () => {
expect(resolveSources).rejects.toThrowError(/^`sources` option must be an array of feed sources or a function that returns it$/)
})
})
})
Loading

0 comments on commit c3f37a4

Please sign in to comment.