Skip to content

Commit

Permalink
Simplify Response
Browse files Browse the repository at this point in the history
Remove `readonly` fields on `Response` to make it easier for
different programming styles to use it.

Have `Response` just be the http-response again. If you want to
return a web socket, use: `Response | WebSocketResponse` explicitly.
  • Loading branch information
tajakobsen committed Feb 1, 2021
1 parent 860b66a commit 591fd00
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 70 deletions.
88 changes: 44 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "enonic-types",
"sideEffects": false,
"version": "0.1.19",
"version": "0.1.20",
"description": "TypeScript types for Enonic XP",
"typings": "index.d.ts",
"scripts": {
Expand All @@ -24,9 +24,9 @@
},
"homepage": "https://github.com/ItemConsulting/enonic-types#readme",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.14.0",
"@typescript-eslint/parser": "^4.14.0",
"eslint": "^7.18.0",
"@typescript-eslint/eslint-plugin": "^4.14.1",
"@typescript-eslint/parser": "^4.14.1",
"eslint": "^7.19.0",
"rimraf": "^3.0.2",
"typescript": "^4.1.3"
}
Expand Down
40 changes: 18 additions & 22 deletions src/controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {XOR} from "./types";

export interface Request {
readonly method: "GET" | "PUT" | "POST" | "DELETE";
readonly scheme: string;
Expand All @@ -23,27 +21,25 @@ export type ResponseType =
| Array<any>
| ReadonlyArray<any>;

export interface HttpResponse<ResponseBody = ResponseType> {
readonly status?: number;
readonly body?: ResponseBody;
readonly contentType?: string;
readonly headers?: { readonly [key: string]: string };
readonly cookies?: { readonly [key: string]: string | Cookie };
readonly redirect?: string;
readonly postProcess?: boolean;
readonly pageContributions?: PageContributions;
readonly applyFilters?: boolean;
export interface Response<ResponseBody extends ResponseType = string> {
status?: number;
body?: ResponseBody;
contentType?: string;
headers?: { readonly [key: string]: string };
cookies?: { readonly [key: string]: string | Cookie };
redirect?: string;
postProcess?: boolean;
pageContributions?: PageContributions;
applyFilters?: boolean;
}

export interface WebSocketResponse<WebSocketData = {}> {
readonly webSocket: {
readonly data?: WebSocketData;
readonly subProtocols?: ReadonlyArray<string>;
webSocket: {
data?: WebSocketData;
subProtocols?: ReadonlyArray<string>;
}
}

export type Response<ResponseBody = ResponseType, WebSocketData = {}> = XOR<HttpResponse<ResponseBody>, WebSocketResponse<WebSocketData>>;

export interface MacroContext<Params = never> {
readonly name: string;
readonly body: string;
Expand All @@ -53,10 +49,10 @@ export interface MacroContext<Params = never> {
}

export interface PageContributions {
readonly headBegin?: string | ReadonlyArray<string>;
readonly headEnd?: string | ReadonlyArray<string>;
readonly bodyBegin?: string | ReadonlyArray<string>;
readonly bodyEnd?: string | ReadonlyArray<string>;
headBegin?: string | ReadonlyArray<string>;
headEnd?: string | ReadonlyArray<string>;
bodyBegin?: string | ReadonlyArray<string>;
bodyEnd?: string | ReadonlyArray<string>;
}

export interface Cookie {
Expand Down Expand Up @@ -109,7 +105,7 @@ export interface CustomSelectorServiceResponseBody {
/**
* This Response can be used by a Service that provides data to a CustomSelector input field
*/
export type CustomSelectorServiceResponse = HttpResponse<CustomSelectorServiceResponseBody>;
export type CustomSelectorServiceResponse = Response<CustomSelectorServiceResponseBody>;

export interface CustomSelectorServiceResponseHit {
readonly id: string;
Expand Down

0 comments on commit 591fd00

Please sign in to comment.