Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
aquapi committed May 5, 2024
1 parent 7d0afa6 commit 96468fc
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 162 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bit-js/byte",
"version": "1.2.5",
"version": "1.2.6",
"module": "index.js",
"devDependencies": {
"@types/bun": "latest",
Expand All @@ -9,7 +9,7 @@
"type": "module",
"types": "types/index.d.ts",
"dependencies": {
"@bit-js/blitz": "^1.2.2"
"@bit-js/blitz": "^1.2.4"
},
"scripts": {
"build-test": "bun build.ts && bun test && tsc --noEmit -p ./tests/tsconfig.json"
Expand Down
161 changes: 1 addition & 160 deletions src/core/server/utils/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,175 +18,16 @@ export interface JsonResponse<T> extends Response {

export type NullableBody = BodyInit | null;

// Default headers and init values
const textHeaders = { 'Content-Type': 'text/plain' };
const textInit = { headers: textHeaders };

const jsonHeaders = { 'Content-Type': 'application/json' };
const jsonInit = { headers: jsonHeaders };

const binaryHeaders = { 'Content-Type': 'application/octet-stream' };
const binaryInit = { headers: binaryHeaders };

const xmlHeaders = { 'Content-Type': 'application/xml' };
const xmlInit = { headers: xmlHeaders };

const htmlHeaders = { 'Content-Type': 'text/html' };
const htmlInit = { headers: htmlHeaders };

const eventHeaders = {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
};
const eventInit = { headers: eventHeaders };

/**
* Basic response format
*/
export const send = {
/**
* Send a `BodyInit` as response
* @deprecated use <code>ctx.body</code> instead
*/
body<const T extends NullableBody>(body: T, init?: CommonResponseInit): BasicResponse<T> {
return typeof init === 'undefined'
? new Response(body) as any
: new Response(body, init) as any;
},

/**
* Send a response with only head
* @deprecated use <code>ctx.head</code> instead
*/
head(init: CommonResponseInit): Response {
return new Response(null, init);
},

/**
* Create a static response handler
*/
static<const T extends NullableBody>(body: T, init?: CommonResponseInit): () => BasicResponse<T> {
return typeof init === 'undefined'
? (): any => new Response(body)
: (): any => new Response(body, init);
},

/**
* Send a primitive value as response
* @deprecated use <code>ctx.value</code> instead
*/
value<const T extends string | number | bigint | boolean | null | undefined>(body: T, init?: CommonResponseInit): BasicResponse<`${T}`> {
return typeof init === 'undefined'
? new Response(`${body}`) as any
: new Response(`${body}`, init) as any;
},

/**
* Redirect to provided href
* @deprecated use <code>ctx.redirect</code> instead
*/
link(Location: string, status: 301 | 302 | 307 | 308): Response {
return new Response(null, { headers: { Location }, status });
},

/**
* Send response as plain text
* @deprecated use <code>ctx.text</code> instead
*/
text<const T extends NullableBody>(body: T, init?: CommonResponseInit): BasicResponse<T> {
if (typeof init === 'undefined')
return new Response(body, textInit) as any;

const { headers } = init;

if (typeof headers === 'undefined') init.headers = textHeaders;
else headers['Content-Type'] = 'text/plain';

return new Response(body, init) as any;
},

/**
* Send response as JSON
* @deprecated use <code>ctx.json</code> instead
*/
json<const T>(body: T, init?: CommonResponseInit): JsonResponse<T> {
if (typeof init === 'undefined')
return new Response(JSON.stringify(body), jsonInit);

const { headers } = init;

if (typeof headers === 'undefined') init.headers = jsonHeaders;
else headers['Content-Type'] = 'application/json';

return new Response(JSON.stringify(body), init);
},

/**
* Send binary response
* @deprecated use <code>ctx.binary</code> instead
*/
binary<const T extends NullableBody>(body: T, init?: CommonResponseInit): BasicResponse<T> {
if (typeof init === 'undefined')
return new Response(body, binaryInit) as any;

const { headers } = init;

if (typeof headers === 'undefined') init.headers = binaryHeaders;
else headers['Content-Type'] = 'application/octet-stream';

return new Response(body, init) as any;
},

/**
* Send XML response
* @deprecated use <code>ctx.xml</code> instead
*/
xml<const T extends NullableBody>(body: T, init?: CommonResponseInit): BasicResponse<T> {
if (typeof init === 'undefined')
return new Response(body, xmlInit) as any;

const { headers } = init;

if (typeof headers === 'undefined') init.headers = xmlHeaders;
else headers['Content-Type'] = 'application/xml';

return new Response(body, init) as any;
},

/**
* Send HTML response
* @deprecated use <code>ctx.html</code> instead
*/
html<const T extends NullableBody>(body: T, init?: CommonResponseInit): BasicResponse<T> {
if (typeof init === 'undefined')
return new Response(body, htmlInit) as any;

const { headers } = init;

if (typeof headers === 'undefined') init.headers = htmlHeaders;
else headers['Content-Type'] = 'text/html';

return new Response(body, init) as any;
},

/**
* Stream server-sent events
* @deprecated use <code>ctx.events</code> instead
*/
events<const T extends NullableBody>(body: T, init?: CommonResponseInit): BasicResponse<T> {
if (typeof init === 'undefined')
return new Response(body, eventInit) as any;

const { headers } = init;

if (typeof headers === 'undefined') init.headers = eventHeaders;
else {
headers['Content-Type'] = 'text/event-stream';
headers['Cache-Control'] = 'no-cache';
headers['Connection'] = 'keep-alive';
};

return new Response(body, init) as any;
},
}
};

0 comments on commit 96468fc

Please sign in to comment.