-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from decentraland/feat/add-more-endpoints
feat: add endpoints for stop and promote
- Loading branch information
Showing
8 changed files
with
201 additions
and
74 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,14 +1,58 @@ | ||
import { HandlerContextWithPath } from '../../types' | ||
import { isErrorWithMessage } from '../../logic/errors' | ||
import { HandlerContextWithPath, StatusCode } from '../../types' | ||
|
||
// handlers arguments only type what they need, to make unit testing easier | ||
export async function squidsHandler(context: Pick<HandlerContextWithPath<'squids', '/squids/list'>, 'url' | 'components'>) { | ||
export async function listSquidsHandler(context: Pick<HandlerContextWithPath<'squids', '/squids/list'>, 'components'>) { | ||
const { | ||
components: { squids } | ||
} = context | ||
|
||
const instances = await squids.list() | ||
|
||
return { | ||
status: StatusCode.OK, | ||
body: instances | ||
} | ||
} | ||
|
||
export async function stopSquidHandler(context: Pick<HandlerContextWithPath<'squids', '/squids/:id/stop'>, 'params' | 'components'>) { | ||
const { | ||
components: { squids }, | ||
params: { id } | ||
} = context | ||
|
||
try { | ||
await squids.downgrade(id) | ||
return { | ||
status: StatusCode.OK | ||
} | ||
} catch (e) { | ||
return { | ||
status: StatusCode.BAD_REQUEST, | ||
body: { | ||
message: isErrorWithMessage(e) ? e.message : 'Could not stop squid' | ||
} | ||
} | ||
} | ||
} | ||
|
||
export async function promoteSquidHandler(context: Pick<HandlerContextWithPath<'squids', '/squids/:id/promote'>, 'params' | 'components'>) { | ||
const { | ||
components: { squids }, | ||
params: { id } | ||
} = context | ||
|
||
try { | ||
await squids.promote(id) | ||
return { | ||
status: StatusCode.OK | ||
} | ||
} catch (e) { | ||
return { | ||
status: StatusCode.BAD_REQUEST, | ||
body: { | ||
message: isErrorWithMessage(e) ? e.message : 'Could not promote squid' | ||
} | ||
} | ||
} | ||
} |
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,12 +1,14 @@ | ||
import { Router } from '@well-known-components/http-server' | ||
import { GlobalContext } from '../types' | ||
import { squidsHandler } from './handlers/squid-handler' | ||
import { listSquidsHandler, promoteSquidHandler, stopSquidHandler } from './handlers/squid-handler' | ||
|
||
// We return the entire router because it will be easier to test than a whole server | ||
export async function setupRouter(): Promise<Router<GlobalContext>> { | ||
const router = new Router<GlobalContext>() | ||
|
||
router.get('/list', squidsHandler) | ||
router.get('/list', listSquidsHandler) | ||
router.put('/:id/promote', promoteSquidHandler) | ||
router.put('/:id/stop', stopSquidHandler) | ||
|
||
return router | ||
} |
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 @@ | ||
export function isErrorWithMessage(error: unknown): error is Error { | ||
return error !== undefined && error !== null && typeof error === 'object' && 'message' in error | ||
} |
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
Oops, something went wrong.