-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registerable.ts
81 lines (76 loc) · 1.96 KB
/
registerable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2021-present the Registerable authors. All rights reserved. MIT license.
import { QUERY_MAP } from "./constants/mod.ts";
import { ifElse, NN } from "./deps.ts";
import { client } from "./api/client.ts";
import { mapper, server, uniqFlatten } from "./cli/_utils.ts";
import { Option, Registry } from "./types/mod.ts";
import { RegisterableResult } from "./types/mod.ts";
const defaultOption: Option = {
mode: "server",
registry: ["deno.land", "nest.land", "npm"],
signal: undefined,
};
/**
* Ask if the name can be registered in the package registry.
*
* @param name - Query name
* @param option - Query option
* @returns Result of registerable or not
*
* @remark
* Never throw an error
*
* @example
* ```ts
* // General usage
* await registerable('fonction')
* // {
* result: {
* "deno.land": false,
* "nest.land": false,
* npm: false,
* },
* hasError: false,
* error: {},
* errorRegistry: [],
* name: "fonction",
* };
* ```
*
* @example
* ```ts
* // Filter query registry
* await registerable('invalid-name', {
* registry: ['deno.land', 'npm']
* })
* // {
* result: {
* "deno.land": false,
* npm: false,
* },
* hasError: true,
* error: {
* "deno.land": "Name contains only the characters a-z, 0-9 and _"
* },
* errorRegistry: ["deno.land"],
* name: "invalid-name",
* };
* ```
*/
const registerable = async <T extends Registry>(
name: string,
option?: Partial<Option<T>>,
): Promise<RegisterableResult<T>> => {
const {
mode = defaultOption.mode,
registry = defaultOption.registry,
signal = defaultOption.signal,
} = option || defaultOption;
const query = uniqFlatten(mapper(QUERY_MAP, registry)).filter((fn) => NN(fn));
return await ifElse(
mode === "server",
async () => await server(query, name),
async () => await client(name, { registry, signal }),
);
};
export { registerable };