-
-
Notifications
You must be signed in to change notification settings - Fork 537
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Response: Adopts functional composition of "res"
- Loading branch information
1 parent
1c34ad5
commit 34e4c69
Showing
15 changed files
with
229 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
__* | ||
.DS_* | ||
node_modules | ||
lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"arrowParens": "always", | ||
"bracketSpacing": true, | ||
"semi": false, | ||
"useTabs": false, | ||
"trailingComma": "all", | ||
"singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"editor.formatOnSave": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import * as R from 'ramda' | ||
|
||
/** | ||
* Sets the given header, or the Map of headers to the response. | ||
* @example | ||
* res(set('Content-Type', 'plain/text')) | ||
* res(set({ 'Content-Type': 'plain/text', 'My-Header': 'foo' })) | ||
*/ | ||
export const set = (name: string | Object, value?: string) => { | ||
return R.ifElse( | ||
R.always(R.is(Object, name)), | ||
R.mergeDeepLeft({ headers: name }), | ||
R.assocPath(['headers', name], value), | ||
) | ||
} | ||
|
||
/** | ||
* Sets a status code and optional status text of the response. | ||
* @example | ||
* res(status(301)) // status code only | ||
* res(status(200, 'Fine')) // with status code | ||
*/ | ||
export const status = (statusCode: number, statusText?: string) => { | ||
return R.compose( | ||
R.assoc('status', statusCode), | ||
R.when( | ||
R.complement(R.always(R.isNil(statusText))), | ||
R.assoc('statusText', statusText), | ||
), | ||
) | ||
} | ||
|
||
/** | ||
* Sets the given text as the body of the response. | ||
* @example | ||
* res(text('Message')) | ||
*/ | ||
export const text = (body: string) => { | ||
return R.compose( | ||
R.assoc('body', body), | ||
set('Content-Type', 'text/plain'), | ||
) | ||
} | ||
|
||
/** | ||
* Sets the given XML as the body of the response. | ||
* @example | ||
* res(xml('<message>Foo</message>')) | ||
*/ | ||
export const xml = (body: string) => { | ||
return R.compose( | ||
R.assoc('body', body), | ||
set('Content-Type', 'text/xml'), | ||
) | ||
} | ||
|
||
/** | ||
* Sets the given Object as the JSON body of the response. | ||
* @example | ||
* res(json({ foo: 'bar' })) | ||
*/ | ||
export const json = (body: Object) => { | ||
return R.compose( | ||
R.assoc('body', JSON.stringify(body)), | ||
set('Content-Type', 'application/json'), | ||
) | ||
} | ||
|
||
/** | ||
* Delays the current response for the given duration (in ms) | ||
* @example | ||
* res(delay(1500), json({ foo: 'bar' })) | ||
*/ | ||
export const delay = (duration: number) => { | ||
return R.assoc('delay', duration) | ||
} |
Oops, something went wrong.