-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
190 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
node_modules/.bin/lint-staged |
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,48 @@ | ||
import { FastifyPluginAsync, FastifyReply } from 'fastify' | ||
|
||
declare namespace fastifyQueue { | ||
export type QueueInitializer<ValueType = unknown, ReturnType = unknown, KeyType = PropertyKey> = ( | ||
key: KeyType, | ||
value: ValueType, | ||
reply: FastifyReply | ||
) => ReturnType | PromiseLike<ReturnType> | ||
|
||
export type QueueResolver<ValueType = unknown, ReturnType = unknown, KeyType = PropertyKey> = ( | ||
key: KeyType, | ||
value: ValueType, | ||
reply: FastifyReply | ||
) => ReturnType | PromiseLike<ReturnType> | ||
|
||
export type OnQueueResolved<ValueType = unknown, KeyType = PropertyKey> = ( | ||
resolvedQueue: Map<KeyType, ValueType>, | ||
reply: FastifyReply | ||
) => void | Promise<void> | ||
|
||
export type QueueInstance <InputType = unknown, KeyType = PropertyKey> = { | ||
add(key: KeyType, value: InputType): void | ||
resolve(cb?: OnQueueResolved): Promise<void> | ||
get contents(): Map<KeyType, PromiseLike<InputType>> | ||
} | ||
|
||
export type QueuePluginOptions = { | ||
initializer?: QueueInitializer | ||
resolver?: QueueResolver | ||
onResolved?: OnQueueResolved | ||
onError?(e: unknown): void | ||
queueName?: string | ||
concurrency?: number | ||
stopOnError?: boolean | ||
} | ||
|
||
const fastifyQueue: FastifyPluginAsync<QueuePluginOptions> | ||
|
||
export { fastifyQueue as default } | ||
} | ||
|
||
declare module 'fastify' { | ||
interface FastifyReply { | ||
queue: fastifyQueue.QueueInstance | ||
} | ||
} | ||
|
||
export = fastifyQueue |
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,103 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import fastify, { FastifyReply } from 'fastify' | ||
// eslint-disable-next-line import/no-unresolved | ||
import { expectAssignable, expectType } from 'tsd' | ||
|
||
import type { | ||
OnQueueResolved, | ||
QueueInitializer, | ||
QueueInstance, | ||
QueuePluginOptions, | ||
QueueResolver | ||
} from '..' | ||
|
||
fastify().get('/', async (req, reply) => { | ||
expectType<QueueInstance>(reply.queue) | ||
|
||
const getBar = async () => 'bar' | ||
expectAssignable<void>(reply.queue.add('foo', getBar())) | ||
|
||
expectType<Map<PropertyKey, PromiseLike<unknown>>>(reply.queue.contents) | ||
|
||
expectAssignable<void>( | ||
await reply.queue.resolve() | ||
) | ||
|
||
expectAssignable<void>( | ||
await reply.queue.resolve( | ||
(resolved, reply) => { | ||
expectAssignable<Map<PropertyKey, unknown>>(resolved) | ||
expectAssignable<FastifyReply>(reply) | ||
} | ||
) | ||
) | ||
}) | ||
|
||
expectType(<QueuePluginOptions>({})) | ||
|
||
expectType(<QueuePluginOptions>({ | ||
initializer: (key, value, reply) => { | ||
expectAssignable<PropertyKey>(key) | ||
expectAssignable<unknown>(value) | ||
expectAssignable<FastifyReply>(reply) | ||
}, | ||
resolver: (key, value, reply) => { | ||
expectAssignable<PropertyKey>(key) | ||
expectAssignable<unknown>(value) | ||
expectAssignable<FastifyReply>(reply) | ||
}, | ||
onResolved: (resolved, reply) => { | ||
expectAssignable<Map<PropertyKey, unknown>>(resolved) | ||
expectAssignable<FastifyReply>(reply) | ||
}, | ||
onError: (e) => e, | ||
queueName: 'myQueue', | ||
concurrency: 9, | ||
stopOnError: true | ||
})) | ||
|
||
expectType(<QueuePluginOptions>({ | ||
initializer: async (key, value, reply) => { | ||
expectAssignable<PropertyKey>(key) | ||
expectAssignable<unknown>(value) | ||
expectAssignable<FastifyReply>(reply) | ||
}, | ||
resolver: async (key, value, reply) => { | ||
expectAssignable<PropertyKey>(key) | ||
expectAssignable<unknown>(value) | ||
expectAssignable<FastifyReply>(reply) | ||
}, | ||
onResolved: async (resolved, reply) => { | ||
expectAssignable<Map<PropertyKey, unknown>>(resolved) | ||
expectAssignable<FastifyReply>(reply) | ||
} | ||
})) | ||
|
||
type KeyType = string | ||
type ValueType = string[] | ||
type ReturnType = Promise<Record<string, string>> | ||
|
||
const initializer: QueueInitializer<ValueType, ReturnType, KeyType> = async (key, value, reply) => { | ||
expectAssignable<KeyType>(key) | ||
expectAssignable<ValueType>(value) | ||
expectAssignable<FastifyReply>(reply) | ||
return { foo: 'bar' } | ||
} | ||
|
||
const resolver: QueueResolver<ValueType, ReturnType, KeyType> = async (key, value, reply) => { | ||
expectAssignable<KeyType>(key) | ||
expectAssignable<ValueType>(value) | ||
expectAssignable<FastifyReply>(reply) | ||
return { bar: 'baz' } | ||
} | ||
|
||
const onResolved: OnQueueResolved<ValueType, KeyType> = async (resolved, reply) => { | ||
expectAssignable<Map<KeyType, ValueType>>(resolved) | ||
expectAssignable<FastifyReply>(reply) | ||
} | ||
|
||
expectType(<QueuePluginOptions>({ | ||
initializer, | ||
resolver, | ||
onResolved | ||
})) |
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