-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: convert to typescript #777
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ logs | |
*.log | ||
|
||
coverage | ||
.coverage | ||
|
||
# Runtime data | ||
pids | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,8 @@ import Joi from 'joi' | |
import boom from '@hapi/boom' | ||
import { logger } from '@libp2p/logger' | ||
import { tmpDir } from '../utils.js' | ||
|
||
/** | ||
* @typedef {import('../types').Factory} Factory | ||
*/ | ||
import type { Server } from '@hapi/hapi' | ||
import type { Factory } from '../index.js' | ||
|
||
const debug = logger('ipfsd-ctl:routes') | ||
|
||
|
@@ -18,12 +16,9 @@ const routeOptions = { | |
} | ||
} | ||
|
||
/** | ||
* @param {Error & { stdout?: string }} err | ||
*/ | ||
const badRequest = err => { | ||
const badRequest = (err: Error & { stdout?: string }) => { | ||
let msg | ||
if (err.stdout) { | ||
if (err.stdout != null) { | ||
msg = err.stdout + ' - ' + err.message | ||
} else { | ||
msg = err.message | ||
|
@@ -32,27 +27,17 @@ const badRequest = err => { | |
throw boom.badRequest(msg) | ||
} | ||
|
||
/** | ||
* @type {Record<string, any>} | ||
*/ | ||
const nodes = {} | ||
|
||
/** | ||
* @namespace EndpointServerRoutes | ||
* @ignore | ||
* @param {import('@hapi/hapi').Server} server | ||
* @param {() => Factory | Promise<Factory>} createFactory | ||
* @returns {void} | ||
*/ | ||
export default (server, createFactory) => { | ||
const nodes: Record<string, any> = {} | ||
|
||
export default (server: Server, createFactory: () => Factory | Promise<Factory>): void => { | ||
server.route({ | ||
method: 'GET', | ||
path: '/util/tmp-dir', | ||
handler: async (request) => { | ||
const type = request.query.type || 'go' | ||
const type = request.query.type ?? 'go' | ||
try { | ||
return { tmpDir: await tmpDir(type) } | ||
} catch (/** @type {any} */ err) { | ||
} catch (err: any) { | ||
badRequest(err) | ||
} | ||
} | ||
|
@@ -66,7 +51,7 @@ export default (server, createFactory) => { | |
|
||
try { | ||
return { version: await nodes[id].version() } | ||
} catch (/** @type {any} */ err) { | ||
} catch (err: any) { | ||
badRequest(err) | ||
} | ||
}, | ||
|
@@ -77,25 +62,25 @@ export default (server, createFactory) => { | |
method: 'POST', | ||
path: '/spawn', | ||
handler: async (request) => { | ||
const opts = request.payload || {} | ||
const opts = request.payload ?? {} | ||
try { | ||
const ipfsd = await createFactory() | ||
const id = nanoid() | ||
// @ts-expect-error opts is a json object | ||
nodes[id] = await ipfsd.spawn(opts) | ||
return { | ||
id: id, | ||
apiAddr: nodes[id].apiAddr ? nodes[id].apiAddr.toString() : '', | ||
gatewayAddr: nodes[id].gatewayAddr ? nodes[id].gatewayAddr.toString() : '', | ||
grpcAddr: nodes[id].grpcAddr ? nodes[id].grpcAddr.toString() : '', | ||
apiAddr: nodes[id].apiAddr?.toString(), | ||
gatewayAddr: nodes[id].gatewayAddr?.toString(), | ||
grpcAddr: nodes[id].grpcAddr?.toString(), | ||
initialized: nodes[id].initialized, | ||
started: nodes[id].started, | ||
disposable: nodes[id].disposable, | ||
env: nodes[id].env, | ||
path: nodes[id].path, | ||
clean: nodes[id].clean | ||
} | ||
} catch (/** @type {any} */ err) { | ||
} catch (err: any) { | ||
badRequest(err) | ||
} | ||
} | ||
|
@@ -109,15 +94,15 @@ export default (server, createFactory) => { | |
path: '/init', | ||
handler: async (request) => { | ||
const id = request.query.id | ||
const payload = request.payload || {} | ||
const payload = request.payload ?? {} | ||
|
||
try { | ||
await nodes[id].init(payload) | ||
|
||
return { | ||
initialized: nodes[id].initialized | ||
} | ||
} catch (/** @type {any} */ err) { | ||
} catch (err: any) { | ||
badRequest(err) | ||
} | ||
}, | ||
|
@@ -137,11 +122,11 @@ export default (server, createFactory) => { | |
await nodes[id].start() | ||
|
||
return { | ||
apiAddr: nodes[id].apiAddr ? nodes[id].apiAddr.toString() : '', | ||
gatewayAddr: nodes[id].gatewayAddr ? nodes[id].gatewayAddr.toString() : '', | ||
grpcAddr: nodes[id].grpcAddr ? nodes[id].grpcAddr.toString() : '' | ||
apiAddr: nodes[id].apiAddr?.toString(), | ||
gatewayAddr: nodes[id].gatewayAddr?.toString(), | ||
grpcAddr: nodes[id].grpcAddr?.toString() | ||
Comment on lines
+125
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we're changing the results from being There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think it's a bit better like as these fields will be stripped out on JSON encode now as they're undefined instead of being an empty string which is not a valid string representation of a multiaddr. |
||
} | ||
} catch (/** @type {any} */ err) { | ||
} catch (err: any) { | ||
badRequest(err) | ||
} | ||
}, | ||
|
@@ -163,7 +148,7 @@ export default (server, createFactory) => { | |
await nodes[id].cleanup() | ||
|
||
return h.response().code(200) | ||
} catch (/** @type {any} */ err) { | ||
} catch (err: any) { | ||
badRequest(err) | ||
} | ||
}, | ||
|
@@ -183,7 +168,7 @@ export default (server, createFactory) => { | |
await nodes[id].stop() | ||
|
||
return h.response().code(200) | ||
} catch (/** @type {any} */ err) { | ||
} catch (err: any) { | ||
badRequest(err) | ||
} | ||
}, | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we're changing the results from being
''
(empty string) when undefined, to beingundefined
. Is this okay?