• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pelotom / runtypes / 7192878195

13 Dec 2023 08:58AM UTC coverage: 95.97%. First build
7192878195

Pull #339

github

web-flow
Merge e544f8f53 into f0b4cfd74
Pull Request #339: refactor!: revise project configurations

311 of 329 branches covered (0.0%)

Branch coverage included in aggregate %.

1213 of 1259 new or added lines in 40 files covered. (96.35%)

1213 of 1259 relevant lines covered (96.35%)

1743.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.59
/src/utils/AsyncContract.ts
1
import Static from "./Static.ts"
2
import { RuntypeBase } from "../Runtype.ts"
3
import ValidationError from "../result/ValidationError.ts"
1✔
4
import FAILURE from "../utils-internal/FAILURE.ts"
1✔
5

6
type AsyncContract<A extends readonly RuntypeBase[], R extends RuntypeBase> = {
7
        enforce(
8
                f: (
9
                        ...args: {
10
                                [K in keyof A]: A[K] extends RuntypeBase ? Static<A[K]> : unknown
11
                        } & ArrayLike<unknown>
12
                ) => Promise<Static<R>>,
13
        ): (
14
                ...args: {
15
                        [K in keyof A]: A[K] extends RuntypeBase ? Static<A[K]> : unknown
16
                } & ArrayLike<unknown>
17
        ) => Promise<Static<R>>
18
}
19

20
const AsyncContract: {
1✔
21
        /**
22
         * Create a function contract.
23
         */
24
        <A extends readonly RuntypeBase[], R extends RuntypeBase>(
25
                ...runtypes: [...A, R]
26
        ): AsyncContract<A, R>
27
} = <A extends readonly RuntypeBase[], R extends RuntypeBase>(
1✔
28
        ...runtypes: [...A, R]
1✔
29
): AsyncContract<A, R> => {
30
        const lastIndex = runtypes.length - 1
5✔
31
        const argRuntypes = runtypes.slice(0, lastIndex) as unknown as A
5✔
32
        const returnRuntype = runtypes[lastIndex] as R
5✔
33
        return {
5✔
34
                enforce:
5✔
35
                        (
5✔
36
                                f: (
5✔
37
                                        ...args: {
38
                                                [K in keyof A]: A[K] extends RuntypeBase ? Static<A[K]> : unknown
39
                                        }
40
                                ) => Promise<Static<R>>,
41
                        ) =>
42
                        (
5✔
43
                                ...args: {
5✔
44
                                        [K in keyof A]: A[K] extends RuntypeBase ? Static<A[K]> : unknown
45
                                }
46
                        ): Promise<Static<R>> => {
47
                                if (args.length < argRuntypes.length) {
13✔
48
                                        const message = `Expected ${argRuntypes.length} arguments but only received ${args.length}`
14✔
49
                                        const failure = FAILURE.ARGUMENT_INCORRECT(message)
14✔
50
                                        throw new ValidationError(failure)
14✔
51
                                }
14✔
NEW
52
                                for (let i = 0; i < argRuntypes.length; i++) argRuntypes[i]!.check(args[i])
×
53
                                const returnedPromise = f(...args)
16✔
54
                                if (!(returnedPromise instanceof Promise)) {
13✔
55
                                        const message = `Expected function to return a promise, but instead got ${returnedPromise}`
14✔
56
                                        const failure = FAILURE.RETURN_INCORRECT(message)
14✔
57
                                        throw new ValidationError(failure)
14✔
58
                                }
14✔
59
                                return returnedPromise.then(returnRuntype.check)
15✔
60
                        },
13✔
61
        }
5✔
62
}
1✔
63

64
export default AsyncContract
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc