-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add resolve resources function
- Loading branch information
1 parent
ac2d8f8
commit c3f37a4
Showing
6 changed files
with
670 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$/) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.