🛬 Simple and lightweight way to create a HTTP request to the world, with more features sprinkled in.
🛫 Simple and lightweight way to create a HTTP request to the world, with more features sprinkled in.
👉 View other branches: 1.x branch | 2.x branch
const { HttpClient, middleware } = require('@augu/orchid');
const orchid = new HttpClient();
orchid
.use(middleware.logging());
orchid
.get('https://floofy.dev')
.then((res) => console.log(res.body()))
.catch(error => console.error(error));
@augu/orchid requires Node.js v14 or higher to use since it uses ES2020 features like optional chaining (?.
)
$ npm install @augu/orchid
Orchid allows to have custom middleware to do whatever you want from a request or response. An example would be:
const { MiddlewareType } = require('@augu/orchid');
module.exports = {
name: 'my.middleware',
// this is a "MultiMiddleware" type
types: [MiddlewareType.Request, MiddlewareType.Response],
init() {
// called when the middleware is added
},
onRequest(req) {
// called when a request has been made
},
onResponse(client, req, res) {
// called when a response is made
// `client` and `req` are added to do whatever
}
};
Orchid allows you to serialize your own data without doing it yourself every time you make a request. Currently, this is only limited
to Response.body()
.
An example on building a XML serializer would look like this:
const { Serializer } = require('@augu/orchid');
module.exports = class XMLSerializer extends Serializer {
constructor() {
super(/application\/xhtml[+]xml/gi);
}
serialize(data) {
const str = data.toString();
return someXMLParser(str);
}
}
Then we inject it into our http client or adding it with orchid#method
// HttpClient
const client = new HttpClient({
serializers: [new XMLSerializer()]
});
// Method function
orchid.get({
serializers: [new XMLSerializer()]
});
All this version really does is add middleware
or agent
(or both!) to the constructor
new HttpClient([]); // adds middleware only
new HttpClient('my agent'); // adds the agent only
new HttpClient([], 'my agent'); // adds middleware and the agent
Now the HttpClient’s constructor is an object like this:
interface HttpClientOptions {
middleware?: Middleware[]; // add any middleware
baseUrl?: string; // Use a base URL
agent?: string; // Adds an agent
}
The HttpClient’s constructor is now like:
interface HttpClientOptions {
middleware?: Middleware[];
defaults?: DefaultRequestOptions;
agent?: string;
}
interface DefaultRequestOptions {
followRedirects?: boolean;
headers?: { [x: string]: any }
timeout?: number;
baseUrl?: string;
}
Read the migration notes for more information.
Read the migration notes for more information.
@augu/orchid is released under the MIT License, read here for more information. ❤️