Skip to content

Commit

Permalink
Formatted types/h* packages with dprint (DefinitelyTyped#66529)
Browse files Browse the repository at this point in the history
* Formatted types/h* packages with dprint

* fix: hapi unhappy

* fix: hapi unhappy

---------

Co-authored-by: johnnyreilly <johnny_reilly@hotmail.com>
  • Loading branch information
JoshuaKGoldberg and johnnyreilly authored Sep 16, 2023
1 parent 4f8cfc8 commit 1e75d3d
Show file tree
Hide file tree
Showing 681 changed files with 14,891 additions and 13,226 deletions.
113 changes: 56 additions & 57 deletions types/h2o2/h2o2-tests.ts
Original file line number Diff line number Diff line change
@@ -1,98 +1,97 @@
import wreck = require('wreck');
import h2o2 = require('h2o2');
import hapi = require('hapi');
import wreck = require("wreck");
import h2o2 = require("h2o2");
import hapi = require("hapi");

async function main() {
const server = new hapi.Server({});
await server.register(h2o2);

server.route({
method: 'GET',
path: '/hproxyoptions',
method: "GET",
path: "/hproxyoptions",
async handler(request, h) {
// ResponseToolkit augmentation
// https://github.com/hapijs/h2o2#hproxyoptions
return h.proxy({ host: 'example.com', port: 80, protocol: 'http' });
}
return h.proxy({ host: "example.com", port: 80, protocol: "http" });
},
});

server.route({
method: 'GET',
path: '/using-the-host-port-protocol-options',
method: "GET",
path: "/using-the-host-port-protocol-options",
handler: {
// HandlerDecorations augmentation
// https://github.com/hapijs/h2o2#using-the-host-port-protocol-options
proxy: {
host: '10.33.33.1',
port: '443',
protocol: 'https'
}
}
host: "10.33.33.1",
port: "443",
protocol: "https",
},
},
});

server.route({
method: 'GET',
path: '/using-the-uri-option',
method: "GET",
path: "/using-the-uri-option",
handler: {
// HandlerDecorations augmentation
// https://github.com/hapijs/h2o2#using-the-uri-option
proxy: {
uri: 'https://some.upstream.service.com/that/has?what=you&want=todo'
}
}
uri: "https://some.upstream.service.com/that/has?what=you&want=todo",
},
},
});


server.route({
method: 'GET',
path: '/custom-uri-template-values',
method: "GET",
path: "/custom-uri-template-values",
handler: {
// HandlerDecorations augmentation
// https://github.com/hapijs/h2o2#custom-uri-template-values
proxy: {
uri: '{protocol}://{host}:{port}/go/to/{path}'
}
}
uri: "{protocol}://{host}:{port}/go/to/{path}",
},
},
});

server.route({
method: 'GET',
path: '/custom-uri-template-values/{bar}',
method: "GET",
path: "/custom-uri-template-values/{bar}",
handler: {
// HandlerDecorations augmentation
// https://github.com/hapijs/h2o2#custom-uri-template-values
proxy: {
uri: 'https://some.upstream.service.com/some/path/to/{bar}'
}
}
uri: "https://some.upstream.service.com/some/path/to/{bar}",
},
},
});

server.route({
method: 'GET',
path: '/',
method: "GET",
path: "/",
handler: {
// HandlerDecorations augmentation
// https://github.com/hapijs/h2o2#using-the-mapuri-and-onresponse-options
proxy: {
async mapUri(request) {
console.log('doing some additional stuff before redirecting');
console.log("doing some additional stuff before redirecting");
return {
uri: 'https://some.upstream.service.com/'
uri: "https://some.upstream.service.com/",
};
},

async onResponse(err, res, request, h, settings, ttl) {
console.log('receiving the response from the upstream.');
console.log("receiving the response from the upstream.");
const payload = await wreck.read(res, { json: true });

console.log('some payload manipulation if you want to.')
console.log("some payload manipulation if you want to.");
let response = h.response(payload);

// TODO find a quicker way to do this
for (let header in res.headers) {
let value = res.headers[header];
if (value) {
if (typeof value === 'string') {
if (typeof value === "string") {
value = [value];
}
for (let v of value) {
Expand All @@ -101,9 +100,9 @@ async function main() {
}
}
return response;
}
}
}
},
},
},
});

await server.start();
Expand All @@ -122,35 +121,35 @@ async function main() {
*/

var proxyOptions: h2o2.ProxyHandlerOptions = {
host: '10.33.33.1',
port: '443',
protocol: 'https' // errors correctly if misspelt
}
host: "10.33.33.1",
port: "443",
protocol: "https", // errors correctly if misspelt
};

const badProtocolDemo: hapi.ServerRoute = {
method: 'GET',
path: '/',
method: "GET",
path: "/",
handler: {
proxy: {
host: '10.33.33.1',
port: '443'
host: "10.33.33.1",
port: "443",
// port: null // detected as incompatible
}
}
},
},
};

const replyViaToolkit: hapi.ServerRoute = {
method: 'GET',
path: '/',
method: "GET",
path: "/",
async handler(req, h): Promise<hapi.ResponseObject> {
return h.proxy({
host: '10.33.33.1',
port: '443',
protocol: 'https'
host: "10.33.33.1",
port: "443",
protocol: "https",
});
}
},
};

if (!module.parent) {
main().then(() => console.log('done'), err => console.error(err.stack));
main().then(() => console.log("done"), err => console.error(err.stack));
}
39 changes: 18 additions & 21 deletions types/h2o2/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@

/// <reference types="node" />

import http = require('http');
import Boom = require('boom');
import http = require("http");
import Boom = require("boom");

import {
Plugin,
Request,
ResponseObject,
ResponseToolkit,
Lifecycle,
RouteOptions,
} from 'hapi';
import { Lifecycle, Plugin, Request, ResponseObject, ResponseToolkit, RouteOptions } from "hapi";

declare namespace h2o2 {
/** `mapURI` return value */
Expand All @@ -35,7 +28,7 @@ declare namespace h2o2 {
/** port - upstream service port. */
port?: number | string | undefined;
/** protocol - protocol to use when making the request to the proxied host: */
protocol?: 'http' | 'https' | undefined;
protocol?: "http" | "https" | undefined;
/** uri - absolute URI used instead of host, port, protocol, path, and query. Cannot be used with host, port, protocol, or mapUri. */
uri?: string | undefined;
/** passThrough - if set to true, it forwards the headers from the client to the upstream service, headers sent from the upstream service will also be forwarded to the client. Defaults to false. */
Expand All @@ -54,7 +47,7 @@ declare namespace h2o2 {
timeout?: number | undefined;
/** mapUri - a function used to map the request URI to the target `uri` and optional `headers` with which to make that request. Cannot be used together with `host`, `port`, `protocol`, or `uri`.
* @param request - is the incoming request object.
*/
*/
mapUri?: ((this: ProxyHandlerOptions, request: Request) => Promise<ProxyTarget>) | undefined;
/**
* onResponse - a custom function for processing the response from the upstream service before sending to the client. Useful for custom error handling of responses from the proxied endpoint or other payload manipulation.
Expand All @@ -65,23 +58,27 @@ declare namespace h2o2 {
* @param settings - the proxy handler configuration.
* @param ttl - the upstream TTL in milliseconds if proxy.ttl it set to 'upstream' and the upstream response included a valid 'Cache-Control' header with 'max-age'.
*/
onResponse?: ((this: RouteOptions,
err: null | Boom,
res: http.IncomingMessage,
req: Request,
h: ResponseToolkit,
settings: ProxyHandlerOptions,
ttl: number) => Lifecycle.ReturnValue) | undefined;
onResponse?:
| ((
this: RouteOptions,
err: null | Boom,
res: http.IncomingMessage,
req: Request,
h: ResponseToolkit,
settings: ProxyHandlerOptions,
ttl: number,
) => Lifecycle.ReturnValue)
| undefined;
/** ttl - if set to 'upstream', applies the upstream response caching policy to the response using the response.ttl() method (or passed as an argument to the onResponse method if provided). */
ttl?: 'upstream' | undefined;
ttl?: "upstream" | undefined;
/** agent - a node http(s) agent to be used for connections to upstream server. @see {@link https://nodejs.org/api/http.html#http_class_http_agent} */
agent?: http.Agent | undefined;
/** maxSockets - sets the maximum number of sockets available per outgoing proxy host connection. false means use the wreck module default value (Infinity). Does not affect non-proxy outgoing client connections. Defaults to Infinity. */
maxSockets?: false | number | undefined;
}
}

declare module 'hapi' {
declare module "hapi" {
interface HandlerDecorations {
/**
* Proxies the request to an upstream endpoint.
Expand Down
Loading

0 comments on commit 1e75d3d

Please sign in to comment.