Skip to content

Commit 05e8246

Browse files
committed
improve HttpRequest event handler parameter structure
fix code formatting
1 parent e70e0ec commit 05e8246

File tree

3 files changed

+51
-35
lines changed

3 files changed

+51
-35
lines changed

types/mokapi/http.d.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,29 +113,7 @@ export function options(url: string, body?: JSONValue, args?: Args): Response;
113113
* maxRedirects: 2
114114
* });
115115
*/
116-
export function fetch(url: string, opts?: FetchOptions): Promise<Response>;
117-
118-
/**
119-
* Request arguments.
120-
* Used to add headers to a request.
121-
*/
122-
export interface Args {
123-
/** Request headers. */
124-
headers?: { [name: string]: string };
125-
/**
126-
* The number of redirects to follow for this request.
127-
* @default 5
128-
*/
129-
maxRedirects?: number;
130-
/**
131-
* Maximum time to wait for the request to complete. Default
132-
* timeout is 60 seconds ("60s"). The type can also be a number, in which
133-
* case Mokapi interprets it as milliseconds
134-
* @example
135-
* const res = get(url, { timeout: '5m' })
136-
*/
137-
timeout?: number | string;
138-
}
116+
export function fetch(url: string, opts?: FetchOptions): Promise<Response>
139117

140118
/**
141119
* Options for the {@link fetch} function.
@@ -187,6 +165,28 @@ export interface FetchOptions {
187165
timeout?: number | string;
188166
}
189167

168+
/**
169+
* Request arguments.
170+
* Used to add headers to a request.
171+
*/
172+
export interface Args {
173+
/** Request headers. */
174+
headers?: { [name: string]: string };
175+
/**
176+
* The number of redirects to follow for this request.
177+
* @default 5
178+
*/
179+
maxRedirects?: number;
180+
/**
181+
* Maximum time to wait for the request to complete. Default
182+
* timeout is 60 seconds ("60s"). The type can also be a number, in which
183+
* case Mokapi interprets it as milliseconds
184+
* @example
185+
* const res = get(url, { timeout: '5m' })
186+
*/
187+
timeout?: number | string;
188+
}
189+
190190
/**
191191
* Response of an HTTP request
192192
* https://mokapi.io/docs/javascript-api/mokapi-http/httpresponse
@@ -209,4 +209,4 @@ export interface Response {
209209
* res.json()
210210
*/
211211
json(): JSONValue;
212-
}
212+
}

types/mokapi/index.d.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ export interface HttpRequest {
162162

163163
/** OperationId defined in OpenAPI */
164164
readonly operationId: string;
165+
166+
/** Returns a string representing this HttpRequest object. */
167+
toString(): string
165168
}
166169

167170
/**
@@ -170,7 +173,7 @@ export interface HttpRequest {
170173
*/
171174
export interface HttpResponse {
172175
/** Object contains header parameters specified by OpenAPI header parameters. */
173-
headers: { [key: string]: string };
176+
headers: { [key: string]: any };
174177

175178
/** Specifies the http status used to select the OpenAPI response definition. */
176179
statusCode: number;
@@ -192,11 +195,17 @@ export interface Url {
192195
/** URL host. */
193196
readonly host: string;
194197

198+
/** URL port */
199+
readonly port: number;
200+
195201
/** URL path. */
196202
readonly path: string;
197203

198204
/** URL query string. */
199205
readonly query: string;
206+
207+
/** Returns a string representing this Url object. */
208+
toString(): string;
200209
}
201210

202211
/**
@@ -429,20 +438,20 @@ export type DateLayout =
429438
*/
430439
export interface EventArgs {
431440
/**
432-
* Optional key-value pairs used to label the event in the dashboard.
441+
* Adds or overrides existing tags used to label the event in dashboard
433442
*/
434443
tags?: { [key: string]: string };
435444

436445
/**
437446
* Set to `true` to enable tracking of this event handler in the dashboard.
438-
* If omitted, Mokapi automatically checks whether the response object has
439-
* been modified and tracks the handler only if a change is detected.
447+
* Set to `false` to disable tracking. If omitted, Mokapi checks the response
448+
* object to determine if the handler changed it, and tracks it accordingly.
440449
*/
441450
track?: boolean;
442451
}
443452

444453
/**
445-
* cheduledEventArgs is an object used by every and cron function.
454+
* ScheduledEventHandler is an object used by every and cron function.
446455
* https://mokapi.io/docs/javascript-api/mokapi/eventhandler/scheduledeventargs
447456
* @example
448457
* export default function() {
@@ -631,4 +640,4 @@ export interface SharedMemory {
631640
* mokapi.log(`Current counter: ${count}`)
632641
* ```
633642
*/
634-
export const shared: SharedMemory;
643+
export const shared: SharedMemory;

types/mokapi/test/mokapi-tests.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import {
1414
LdapSearchResponse,
1515
on,
1616
patch,
17-
shared,
1817
sleep,
1918
SmtpEventHandler,
2019
SmtpEventMessage,
20+
shared
2121
} from "mokapi";
2222

2323
const handler = () => {};
@@ -47,9 +47,17 @@ on("http", handler, {});
4747
on("http", handler, { tags: { foo: "bar" } });
4848
on("http", handler, { track: true });
4949
on("http", async () => {});
50-
on("http", (request) => {
51-
request.querystring;
52-
});
50+
on("http", (request) => { request.querystring });
51+
on("http", (request, response) => {
52+
const s = request.toString();
53+
const url = request.url.toString();
54+
55+
response.headers = {
56+
"Content-Type": "application/json",
57+
}
58+
response.headers["Access-Control-Allow-Origin"] = "*";
59+
response.headers["foo"] = { bar: 123 };
60+
})
5361

5462
// @ts-expect-error
5563
every(12, () => {});
@@ -134,7 +142,6 @@ h = (req: HttpRequest, res: HttpResponse) => {
134142
res.data = 12;
135143
res.data = "foo";
136144
res.data = {};
137-
// @ts-expect-error
138145
res.headers.foo = 12;
139146
res.headers.foo = "bar";
140147
res.headers["Content-Type"] = "application/json";

0 commit comments

Comments
 (0)