Skip to content

Commit

Permalink
feat: added header sugar syntax
Browse files Browse the repository at this point in the history
still wip
  • Loading branch information
eddienubes committed Feb 10, 2024
1 parent 62e3dca commit 373d5c5
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 12 deletions.
25 changes: 25 additions & 0 deletions src/Sage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,31 @@ export class Sage {
return this;
}

/**
* Sets the Authorization header to base64 of Bearer token with a Basic Prefix.
* @param username
* @param password
*/
basic(username: string, password: string): this {
const encoded = Buffer.from(`${username}:${password}`).toString('base64');
this.set('Authorization', `Basic ${encoded}`);
return this;
}

/**
* Sets the Authorization header to Bearer token. The prefix will be added.
* @param token
*/
bearer(token: string): this {
this.set('Authorization', `Bearer ${token}`);
return this;
}

cookie(key: string, value: string): this {
this.set('Cookie', `${key}=${value}`);
return this;
}

/**
* Method is designed to work only with FormData requests.
* Cannot be combined with .send().
Expand Down
3 changes: 2 additions & 1 deletion src/SageHttpRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HttpMethod } from './types.js';
import { FormData } from 'undici';
import { IncomingHttpHeaders } from 'undici/types/header.js';

export interface SageHttpRequest {
method?: HttpMethod;
Expand All @@ -8,6 +9,6 @@ export interface SageHttpRequest {

// Should always be a string since Undici doesn't support other types
body?: string;
headers?: Record<string, string>;
headers?: IncomingHttpHeaders;
formData?: FormData;
}
2 changes: 0 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { HTTP_STATUS_TO_MESSAGE, HttpStatusText } from './constants.js';
import { Readable } from 'node:stream';
import path from 'node:path';
import { ReadStream } from 'node:fs';
import { Server } from 'node:http';
import { AddressInfo } from 'node:net';

export const serializeToString = (value: unknown): string => {
const result = JSON.stringify(value);
Expand Down
94 changes: 91 additions & 3 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const expectedExpressResponse: SageHttpResponse = {
connection: 'close'
},
body: {
reqHeaders: {
connection: 'close',
'content-type': expect.any(String),
host: expect.stringContaining('localhost'),
'transfer-encoding': 'chunked'
},
message: 'Success!',
body: {},
query: {},
Expand All @@ -39,6 +45,12 @@ const expectedFastifyResponse: SageHttpResponse = {
connection: 'close'
},
body: {
reqHeaders: {
connection: 'close',
'content-type': expect.any(String),
host: expect.stringContaining('localhost'),
'transfer-encoding': 'chunked'
},
message: 'Success!',
body: {},
query: {},
Expand Down Expand Up @@ -142,6 +154,11 @@ describe('request', () => {
...expectedExpressResponse,
body: {
...expectedExpressResponse.body,
reqHeaders: {
...expectedExpressResponse.body.reqHeaders,
'content-length': '4877572',
'transfer-encoding': undefined
},
files: {
picture: [
{
Expand Down Expand Up @@ -173,6 +190,12 @@ describe('request', () => {
...expectedExpressResponse,
body: {
...expectedExpressResponse.body,
reqHeaders: {
connection: 'close',
'content-length': expect.any(String),
host: expect.stringContaining('localhost'),
'content-type': expect.any(String)
},
files: {
picture: [
{
Expand Down Expand Up @@ -201,6 +224,12 @@ describe('request', () => {
...expectedExpressResponse,
body: {
...expectedExpressResponse.body,
reqHeaders: {
connection: 'close',
'content-length': expect.any(String),
host: expect.stringContaining('localhost'),
'content-type': expect.any(String)
},
files: {
picture: [
{
Expand Down Expand Up @@ -241,6 +270,12 @@ describe('request', () => {
array3: ['value5', 'value6'],
key: 'value'
},
reqHeaders: {
connection: 'close',
'content-length': expect.any(String),
host: expect.stringContaining('localhost'),
'content-type': expect.any(String)
},
files: {
picture: [
{
Expand Down Expand Up @@ -286,6 +321,12 @@ describe('request', () => {
connection: 'close'
},
body: {
reqHeaders: {
connection: 'close',
'content-length': expect.any(String),
'content-type': 'application/json',
host: expect.stringContaining('localhost')
},
message: 'Success!',
body: {
data: 'somevalue',
Expand All @@ -297,7 +338,7 @@ describe('request', () => {
key: 'value'
}
},
text: `{"message":"Success!","body":{"data":"somevalue","nestedObj":{"nestedKey":"nestedValue"}},"query":{"key":"value"}}`,
text: expect.any(String),
ok: true,
redirect: false,
location: undefined,
Expand Down Expand Up @@ -344,7 +385,15 @@ describe('request', () => {
.post('/upload')
.attach('picture', 'test/fixtures/cat.jpg');

expect(res).toEqual(expectedFastifyResponse);
expect(res).toEqual({
...expectedFastifyResponse,
body: {
...expectedFastifyResponse.body,
reqHeaders: {
...expectedFastifyResponse.body.reqHeaders
}
}
});
});

it('should work with streams', async () => {
Expand All @@ -353,7 +402,15 @@ describe('request', () => {
.post('/upload')
.attach('picture', stream);

expect(res).toEqual(expectedFastifyResponse);
expect(res).toEqual({
...expectedFastifyResponse,
body: {
...expectedFastifyResponse.body,
reqHeaders: {
...expectedFastifyResponse.body.reqHeaders
}
}
});
});

it('should work with blobs', async () => {
Expand All @@ -366,6 +423,12 @@ describe('request', () => {
...expectedFastifyResponse,
body: {
...expectedFastifyResponse.body,
reqHeaders: {
...expectedFastifyResponse.body.reqHeaders,
'content-length': expect.any(String),
host: expect.stringContaining('localhost'),
'transfer-encoding': undefined
},
files: [
{
encoding: '7bit',
Expand Down Expand Up @@ -393,6 +456,12 @@ describe('request', () => {
...expectedFastifyResponse,
body: {
...expectedFastifyResponse.body,
reqHeaders: {
...expectedFastifyResponse.body.reqHeaders,
'content-length': expect.any(String),
host: expect.stringContaining('localhost'),
'transfer-encoding': undefined
},
files: [
{
filename: 'cat.jpg',
Expand All @@ -417,6 +486,12 @@ describe('request', () => {
...expectedFastifyResponse,
body: {
...expectedFastifyResponse.body,
reqHeaders: {
...expectedFastifyResponse.body.reqHeaders,
'content-length': expect.any(String),
host: expect.stringContaining('localhost'),
'transfer-encoding': undefined
},
files: [
{
encoding: '7bit',
Expand Down Expand Up @@ -447,6 +522,12 @@ describe('request', () => {
...expectedFastifyResponse,
body: {
...expectedFastifyResponse.body,
reqHeaders: {
...expectedFastifyResponse.body.reqHeaders,
'content-length': expect.any(String),
host: expect.stringContaining('localhost'),
'transfer-encoding': undefined
},
files: [
{
encoding: '7bit',
Expand Down Expand Up @@ -479,6 +560,13 @@ describe('request', () => {
expect(res).toEqual({
...expectedFastifyResponse,
body: {
reqHeaders: {
...expectedFastifyResponse.body.reqHeaders,
'content-type': 'application/json',
'content-length': expect.any(String),
host: expect.stringContaining('localhost'),
'transfer-encoding': undefined
},
body: {
data: 'somevalue',
nestedObj: {
Expand Down
20 changes: 14 additions & 6 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const getExpressApp = (): Express => {
res.json({
message: 'Success!',
body: req.body,
query: req.query
query: req.query,
reqHeaders: req.headers
});
});

Expand All @@ -44,7 +45,8 @@ export const getExpressApp = (): Express => {
message: 'Success!',
body: req.body,
query: req.query,
files: req.files
files: req.files,
reqHeaders: req.headers
});
}
);
Expand All @@ -54,7 +56,8 @@ export const getExpressApp = (): Express => {
res.status(500).json({
message: 'Internal Server Error!',
body: req.body,
query: req.query
query: req.query,
reqHeaders: req.headers
});
};

Expand All @@ -74,7 +77,8 @@ export const getFastifyApp = (): FastifyInstance => {
reply.send({
message: 'Success!',
body: request.body || {},
query: request.query || {}
query: request.query || {},
reqHeaders: request.headers
});
});

Expand Down Expand Up @@ -113,15 +117,19 @@ export const getFastifyApp = (): FastifyInstance => {
message: 'Success!',
body: request.body || {},
query: request.query || {},
files: files
files: files,
reqHeaders: request.headers
});
});

fastify.setErrorHandler(
(error: FastifyError, request: FastifyRequest, reply: FastifyReply) => {
console.error(error.stack);
reply.send({
message: 'Internal Server Error!'
message: 'Internal Server Error!',
body: request.body || {},
query: request.query || {},
reqHeaders: request.headers
});
}
);
Expand Down

0 comments on commit 373d5c5

Please sign in to comment.