This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: route http methods to relevant functions (#8)
* feat: route http methods to relevant functions * test: route http methods to relevant functions * chore(packages): update deps
- Loading branch information
Burak Tasci
authored
Sep 30, 2017
1 parent
5c0cdfc
commit 1629980
Showing
5 changed files
with
280 additions
and
57 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
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,20 +1,127 @@ | ||
import { Context, HttpRequest, HttpResponse } from 'azure-functions-ts-essentials'; | ||
import { Context, HttpMethod, HttpRequest, HttpResponse, HttpStatusCode } from 'azure-functions-ts-essentials'; | ||
|
||
const OBJECT_NAME = 'someObject'; | ||
|
||
const getOne = (id: any) => { | ||
return { | ||
status: HttpStatusCode.OK, | ||
body: { | ||
id, | ||
object: OBJECT_NAME, | ||
...TEST_REQUEST_BODY | ||
} | ||
}; | ||
}; | ||
|
||
const getMany = (req: HttpRequest) => { | ||
return { | ||
status: HttpStatusCode.OK, | ||
body: { | ||
object: 'list', | ||
data: [ | ||
{ | ||
id: TEST_ID, | ||
object: OBJECT_NAME, | ||
...TEST_REQUEST_BODY | ||
} | ||
], | ||
url: '/some-function', | ||
hasMore: false, | ||
totalCount: 1 | ||
} | ||
}; | ||
}; | ||
|
||
const insertOne = (req: HttpRequest) => { | ||
return { | ||
status: HttpStatusCode.OK, | ||
body: { | ||
id: TEST_ID, | ||
object: OBJECT_NAME, | ||
...req.body | ||
} | ||
}; | ||
}; | ||
|
||
const patchOne = (req: HttpRequest, id: any) => { | ||
return { | ||
status: HttpStatusCode.MethodNotAllowed, | ||
body: { | ||
error: { | ||
type: 'not_supported', | ||
message: 'PATCH operations are not supported.' | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
const updateOne = (req: HttpRequest, id: any) => { | ||
return { | ||
status: HttpStatusCode.OK, | ||
body: { | ||
id, | ||
object: OBJECT_NAME, | ||
...req.body | ||
} | ||
}; | ||
}; | ||
|
||
const deleteOne = (id: any) => { | ||
return { | ||
status: HttpStatusCode.OK, | ||
body: { | ||
deleted: true, | ||
id | ||
} | ||
}; | ||
}; | ||
|
||
export const TEST_ID = '57ade20771e59f422cc652d9'; | ||
export const TEST_REQUEST_BODY: { name: string } = { | ||
name: 'Azure' | ||
}; | ||
|
||
/** | ||
* Routes the request to the default controller using the relevant method. | ||
* | ||
* @param {Context} context | ||
* @param {HttpRequest} req | ||
* @returns {any} | ||
*/ | ||
export function run(context: Context, req: HttpRequest): any { | ||
context.log('TypeScript HTTP trigger function processed a request.'); | ||
|
||
let res: HttpResponse | null = null; | ||
|
||
if (req.query.name || (req.body && req.body.name)) | ||
res = { | ||
status: 200, | ||
body: `Hello ${req.query.name || req.body.name}` | ||
}; | ||
else | ||
res = { | ||
status: 400, | ||
body: 'Please pass a name on the query string or in the request body' | ||
}; | ||
let res: HttpResponse; | ||
const id = req.params.id; | ||
|
||
switch (req.method) { | ||
case HttpMethod.Get: | ||
res = id | ||
? getOne(id) | ||
: getMany(req); | ||
break; | ||
case HttpMethod.Post: | ||
res = insertOne(req); | ||
break; | ||
case HttpMethod.Patch: | ||
res = patchOne(req, id); | ||
break; | ||
case HttpMethod.Put: | ||
res = updateOne(req, id); | ||
break; | ||
case HttpMethod.Delete: | ||
res = deleteOne(id); | ||
break; | ||
|
||
default: | ||
res = { | ||
status: HttpStatusCode.MethodNotAllowed, | ||
body: { | ||
error: { | ||
type: 'not_supported', | ||
message: `Method ${req.method} not supported.` | ||
} | ||
} | ||
}; | ||
} | ||
|
||
context.done(undefined, res); | ||
} |
Oops, something went wrong.