Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sse and eventsource for serving updates
- Loading branch information
Showing
11 changed files
with
158 additions
and
52 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,54 @@ | ||
import * as qs from 'querystring'; | ||
import axios from 'axios'; | ||
import ora from 'ora'; | ||
|
||
const spinner = ora(); | ||
import EventSource from 'eventsource'; | ||
|
||
export type RequestParams = Record<string, any>; | ||
|
||
export type ResponseParams = Record<string, any>; | ||
const spinner = ora(); | ||
|
||
const createGeneralRequest = (agent: Function) => async (...args: any[]) => { | ||
try { | ||
let response = await agent(...args); | ||
if (response.data.status === true) { | ||
return response.data.data; | ||
} | ||
} catch (err) { | ||
if (err?.response?.data?.message) { | ||
spinner.fail(err.response.data.message); | ||
process.exit(); | ||
} else { | ||
throw err; | ||
} | ||
class EventSourceError extends TypeError { | ||
event: Event; | ||
constructor(e: Event) { | ||
super('event source error event'); | ||
this.event = e; | ||
} | ||
}; | ||
} | ||
|
||
function createGeneralRequest(agent: Function): Function { | ||
return async (...args: any[]) => { | ||
try { | ||
let response = await agent(...args); | ||
if (response.data.status === true) { | ||
return response.data.data; | ||
} | ||
} catch (err) { | ||
if (err?.response?.data?.message) { | ||
spinner.fail(err.response.data.message); | ||
process.exit(); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
export const get = async (host: string, params?: RequestParams) => createGeneralRequest(axios.get)(host, params); | ||
export const post = async (host: string, body?: RequestParams, params?: RequestParams) => createGeneralRequest(axios.post)(host, body, params); | ||
export const put = async (host: string, body?: RequestParams, params?: RequestParams) => createGeneralRequest(axios.put)(host, body, params); | ||
export const remove = async (host: string) => createGeneralRequest(axios.delete)(host); | ||
export const listen = async (host: string, params?: RequestParams): Promise<EventSource> => { | ||
const uri = `${host}?${qs.stringify({ verbose: 1, ...params })}`; | ||
const es = new EventSource(uri); | ||
return new Promise((resolve, reject) => { | ||
es.addEventListener('error', (e: Event) => { | ||
reject(new EventSourceError(e)); | ||
}); | ||
es.addEventListener('session', (e: MessageEvent) => { | ||
if (e.data === 'close') { | ||
es.close(); | ||
} | ||
}); | ||
resolve(es); | ||
}); | ||
}; |
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
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