nestia 0.3.2 → 0.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
package/src/bin/nestia.ts
CHANGED
|
File without changes
|
|
@@ -20,7 +20,14 @@ export class Fetcher
|
|
|
20
20
|
public static fetch<Output>(connection: IConnection, config: Fetcher.IConfig, method: "GET" | "DELETE", path: string): Promise<Primitive<Output>>;
|
|
21
21
|
public static fetch<Input, Output>(connection: IConnection, config: Fetcher.IConfig, method: "POST" | "PUT" | "PATCH", path: string, input: Input): Promise<Primitive<Output>>;
|
|
22
22
|
|
|
23
|
-
public static async fetch<Output>
|
|
23
|
+
public static async fetch<Output>
|
|
24
|
+
(
|
|
25
|
+
connection: IConnection,
|
|
26
|
+
config: Fetcher.IConfig,
|
|
27
|
+
method: string,
|
|
28
|
+
path: string,
|
|
29
|
+
input?: object
|
|
30
|
+
): Promise<Primitive<Output>>
|
|
24
31
|
{
|
|
25
32
|
if (config.input_encrypted === true || config.output_encrypted === true)
|
|
26
33
|
if (connection.encryption === undefined)
|
|
@@ -57,12 +64,16 @@ export class Fetcher
|
|
|
57
64
|
//----
|
|
58
65
|
// RESPONSE MESSAGE
|
|
59
66
|
//----
|
|
60
|
-
//
|
|
61
|
-
if (path[0] !== "/")
|
|
62
|
-
path =
|
|
67
|
+
// URL SPECIFICATION
|
|
68
|
+
if (connection.host[connection.host.length - 1] !== "/" && path[0] !== "/")
|
|
69
|
+
path = "/" + path;
|
|
70
|
+
if (connection.path)
|
|
71
|
+
path = connection.path(path);
|
|
72
|
+
|
|
73
|
+
const url: URL = new URL(`${connection.host}${path}`);
|
|
63
74
|
|
|
64
75
|
// DO FETCH
|
|
65
|
-
const response: Response = await fetch(
|
|
76
|
+
const response: Response = await fetch(url.href, init);
|
|
66
77
|
let content: string = await response.text();
|
|
67
78
|
|
|
68
79
|
if (!content)
|
|
@@ -2,7 +2,6 @@ import type * as tsc from "typescript";
|
|
|
2
2
|
import { Pair } from "tstl/utility/Pair";
|
|
3
3
|
import { Vector } from "tstl/container/Vector";
|
|
4
4
|
|
|
5
|
-
import { Fetcher } from "../bundle/__internal/Fetcher";
|
|
6
5
|
import { IRoute } from "../structures/IRoute";
|
|
7
6
|
|
|
8
7
|
export namespace FunctionGenerator
|
|
@@ -23,22 +22,14 @@ export namespace FunctionGenerator
|
|
|
23
22
|
--------------------------------------------------------- */
|
|
24
23
|
function body(route: IRoute, query: IRoute.IParameter | undefined, input: IRoute.IParameter | undefined): string
|
|
25
24
|
{
|
|
26
|
-
// PATH WITH ENCRYPTION
|
|
27
|
-
const path: string = get_path(route, query);
|
|
28
|
-
const config: Fetcher.IConfig = {
|
|
29
|
-
input_encrypted: input !== undefined && input.encrypted,
|
|
30
|
-
output_encrypted: route.encrypted
|
|
31
|
-
};
|
|
32
|
-
|
|
33
25
|
// FETCH ARGUMENTS WITH REQUST BODY
|
|
26
|
+
const [parameters] = get_path(route, query);
|
|
34
27
|
const fetchArguments: string[] =
|
|
35
28
|
[
|
|
36
29
|
"connection",
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
`"${route.method}"`,
|
|
41
|
-
path
|
|
30
|
+
`${route.name}.CONFIG`,
|
|
31
|
+
`${route.name}.METHOD`,
|
|
32
|
+
`${route.name}.path(${parameters.map(p => p.name).join(", ")})`
|
|
42
33
|
];
|
|
43
34
|
if (input !== undefined)
|
|
44
35
|
fetchArguments.push("input");
|
|
@@ -52,18 +43,20 @@ export namespace FunctionGenerator
|
|
|
52
43
|
+ "}";
|
|
53
44
|
}
|
|
54
45
|
|
|
55
|
-
function get_path(route: IRoute, query: IRoute.IParameter | undefined): string
|
|
46
|
+
function get_path(route: IRoute, query: IRoute.IParameter | undefined): [IRoute.IParameter[], string]
|
|
56
47
|
{
|
|
57
48
|
const parameters = route.parameters.filter(param => param.category === "param");
|
|
58
|
-
let path: string = route.path;
|
|
59
49
|
|
|
50
|
+
let path: string = route.path;
|
|
60
51
|
for (const param of parameters)
|
|
61
52
|
path = path.replace(`:${param.field}`, `\${${param.name}}`);
|
|
62
|
-
|
|
63
|
-
new URLSearchParams()
|
|
64
|
-
return (query !== undefined)
|
|
53
|
+
path = (query !== undefined)
|
|
65
54
|
? `\`${path}?\${new URLSearchParams(${query.name} as any).toString()}\``
|
|
66
|
-
: `\`${path}
|
|
55
|
+
: `\`${path}\``;
|
|
56
|
+
|
|
57
|
+
if (query)
|
|
58
|
+
parameters.push(query);
|
|
59
|
+
return [parameters, path];
|
|
67
60
|
}
|
|
68
61
|
|
|
69
62
|
/* ---------------------------------------------------------
|
|
@@ -157,12 +150,28 @@ export namespace FunctionGenerator
|
|
|
157
150
|
if (route.output !== "void")
|
|
158
151
|
types.push(new Pair("Output", route.output));
|
|
159
152
|
|
|
160
|
-
|
|
161
|
-
return null;
|
|
162
|
-
|
|
153
|
+
const [parameters, path] = get_path(route, query);
|
|
163
154
|
return `export namespace ${route.name}\n`
|
|
164
155
|
+ "{\n"
|
|
165
|
-
+
|
|
156
|
+
+
|
|
157
|
+
(
|
|
158
|
+
types.length !== 0
|
|
159
|
+
? types.map(tuple => ` export type ${tuple.first} = Primitive<${tuple.second}>;`)
|
|
160
|
+
.join("\n")
|
|
161
|
+
+ "\n\n"
|
|
162
|
+
: ""
|
|
163
|
+
)
|
|
164
|
+
+ ` export const METHOD = "${route.method}";\n`
|
|
165
|
+
+ ` export const PATH = "${route.path}";\n`
|
|
166
|
+
+ ` export const CONFIG = {\n`
|
|
167
|
+
+ ` input_encrypted: ${input !== undefined && input.encrypted},\n`
|
|
168
|
+
+ ` output_encrypted: ${route.encrypted},\n`
|
|
169
|
+
+ ` };\n`
|
|
170
|
+
+ "\n"
|
|
171
|
+
+ ` export function path(${parameters.map(param => `${param.name}: ${param.type}`).join(", ")}): string\n`
|
|
172
|
+
+ ` {\n`
|
|
173
|
+
+ ` return ${path};\n`
|
|
174
|
+
+ ` }\n`
|
|
166
175
|
+ "}";
|
|
167
176
|
}
|
|
168
177
|
}
|