Which type do I return with AWS lambda responses in typescript to suite AWS APIGateway Method Response?
I want to build a proper typescript project on AWS lambda.
Right now, I am having the following definitions:
export type HttpResponse = {
statusCode: number;
headers: {};
body: string;
}
export async function readCollection (event, context, callback): Promise<HttpResponse>{
console.log(event); // Contains incoming request data (e.g., query params, headers and more)
const data = [
{
id: "a7b5bf50-0b5b-11e9-bc65-6bfc39f23288",
name: "some thing",
uri: `/notifications/a7b5bf50-0b5b-11e9-bc65-6bfc39f23288`
}
]
const response = {
statusCode: 200,
headers: {
},
body: JSON.stringify({
status: "ok",
data: data
})
};
return response;
};
But
Instead of my custom HttpResponse
type, I want to use an official definition.
But which official type do I import and return?
typescript aws-lambda aws-api-gateway
add a comment |
I want to build a proper typescript project on AWS lambda.
Right now, I am having the following definitions:
export type HttpResponse = {
statusCode: number;
headers: {};
body: string;
}
export async function readCollection (event, context, callback): Promise<HttpResponse>{
console.log(event); // Contains incoming request data (e.g., query params, headers and more)
const data = [
{
id: "a7b5bf50-0b5b-11e9-bc65-6bfc39f23288",
name: "some thing",
uri: `/notifications/a7b5bf50-0b5b-11e9-bc65-6bfc39f23288`
}
]
const response = {
statusCode: 200,
headers: {
},
body: JSON.stringify({
status: "ok",
data: data
})
};
return response;
};
But
Instead of my custom HttpResponse
type, I want to use an official definition.
But which official type do I import and return?
typescript aws-lambda aws-api-gateway
I don't think there is an "official" types from AWS, you may find community ones. Also I think that what your looking for is API Gateway definition, as lambda can return whatever you want based on what you do with it.
– Boris Charpentier
Dec 30 '18 at 11:00
True .. IT is more 'api gateway method response type ' ;)
– wzr1337
Dec 30 '18 at 11:26
add a comment |
I want to build a proper typescript project on AWS lambda.
Right now, I am having the following definitions:
export type HttpResponse = {
statusCode: number;
headers: {};
body: string;
}
export async function readCollection (event, context, callback): Promise<HttpResponse>{
console.log(event); // Contains incoming request data (e.g., query params, headers and more)
const data = [
{
id: "a7b5bf50-0b5b-11e9-bc65-6bfc39f23288",
name: "some thing",
uri: `/notifications/a7b5bf50-0b5b-11e9-bc65-6bfc39f23288`
}
]
const response = {
statusCode: 200,
headers: {
},
body: JSON.stringify({
status: "ok",
data: data
})
};
return response;
};
But
Instead of my custom HttpResponse
type, I want to use an official definition.
But which official type do I import and return?
typescript aws-lambda aws-api-gateway
I want to build a proper typescript project on AWS lambda.
Right now, I am having the following definitions:
export type HttpResponse = {
statusCode: number;
headers: {};
body: string;
}
export async function readCollection (event, context, callback): Promise<HttpResponse>{
console.log(event); // Contains incoming request data (e.g., query params, headers and more)
const data = [
{
id: "a7b5bf50-0b5b-11e9-bc65-6bfc39f23288",
name: "some thing",
uri: `/notifications/a7b5bf50-0b5b-11e9-bc65-6bfc39f23288`
}
]
const response = {
statusCode: 200,
headers: {
},
body: JSON.stringify({
status: "ok",
data: data
})
};
return response;
};
But
Instead of my custom HttpResponse
type, I want to use an official definition.
But which official type do I import and return?
typescript aws-lambda aws-api-gateway
typescript aws-lambda aws-api-gateway
edited Jan 2 at 13:33
wzr1337
asked Dec 30 '18 at 9:03
wzr1337wzr1337
1,74931934
1,74931934
I don't think there is an "official" types from AWS, you may find community ones. Also I think that what your looking for is API Gateway definition, as lambda can return whatever you want based on what you do with it.
– Boris Charpentier
Dec 30 '18 at 11:00
True .. IT is more 'api gateway method response type ' ;)
– wzr1337
Dec 30 '18 at 11:26
add a comment |
I don't think there is an "official" types from AWS, you may find community ones. Also I think that what your looking for is API Gateway definition, as lambda can return whatever you want based on what you do with it.
– Boris Charpentier
Dec 30 '18 at 11:00
True .. IT is more 'api gateway method response type ' ;)
– wzr1337
Dec 30 '18 at 11:26
I don't think there is an "official" types from AWS, you may find community ones. Also I think that what your looking for is API Gateway definition, as lambda can return whatever you want based on what you do with it.
– Boris Charpentier
Dec 30 '18 at 11:00
I don't think there is an "official" types from AWS, you may find community ones. Also I think that what your looking for is API Gateway definition, as lambda can return whatever you want based on what you do with it.
– Boris Charpentier
Dec 30 '18 at 11:00
True .. IT is more 'api gateway method response type ' ;)
– wzr1337
Dec 30 '18 at 11:26
True .. IT is more 'api gateway method response type ' ;)
– wzr1337
Dec 30 '18 at 11:26
add a comment |
3 Answers
3
active
oldest
votes
After days of research I found the answer so close ;)
you return Promise<APIGateway.MethodResponse>
import { APIGateway } from "aws-sdk";
export async function readCollection (event, context, callback): Promise<APIGateway.MethodResponse> {
console.log(event); // Contains incoming request data (e.g., query params, headers and more)
const data = [
{
id: "a7b5bf50-0b5b-11e9-bc65-6bfc39f23288",
name: "some thing",
uri: `/notifications/a7b5bf50-0b5b-11e9-bc65-6bfc39f23288`
}
]
const response = {
statusCode: "200",
headers: {
},
body: JSON.stringify({
status: "ok",
data: data
})
};
return response;
};
Nice. I have upvoted your answer and removed mine. I have been using Lambda for several years now but couldn't find about this. Great work @wzr1337
– Ashan
Jan 2 at 6:45
Actually this is incorrect. The "aws-sdk" package is providing types for the AWS Client SDK and is importing the type from github.com/aws/aws-sdk-js/blob/… but this is the API Gateway control plane REST API used to create/modify an actual API in API Gateway as documented at docs.aws.amazon.com/apigateway/api-reference/resource/… . See my answer below for an alternative (and correct) way to accomplish this.
– Scott Willeke
Jan 19 at 5:05
add a comment |
The @types/aws-lambda package provides types for using TypeScript inside of an AWS Lambda function. For AWS Lambda functions invoked using API Gateway's Lambda Proxy integration type take the following steps:
Install the package
$ yarn add --dev @types/aws-lambda
Or if you prefer npm:
$ npm i --save-dev @types/aws-lambda
Then in your handler file:
import { APIGatewayProxyEvent, Context, APIGatewayProxyResult } from "aws-lambda"
export async function hello (event: APIGatewayProxyEvent, context: Context): Promise<APIGatewayProxyResult> {
return {
statusCode: 200,
body: JSON.stringify({
message: 'Hello world',
input: event,
})
}
}
add a comment |
You need to return a callback otherwise if your lambda was triggered by an s3 put it will think it did not execute and will continue executing your function from time to time generating duplicated calls. You also need to return a callback from an API Gateway request so you can send a proper response to the user.
For success calls:
callback(null, response);
For error calls:
callback(error, null);
The above code works just fine returning a promise.. you do not need the callback
– wzr1337
Dec 30 '18 at 13:13
The question is: whicht Typescript type do I specify as return value of the promise
– wzr1337
Dec 30 '18 at 13:14
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53976371%2fwhich-type-do-i-return-with-aws-lambda-responses-in-typescript-to-suite-aws-apig%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
After days of research I found the answer so close ;)
you return Promise<APIGateway.MethodResponse>
import { APIGateway } from "aws-sdk";
export async function readCollection (event, context, callback): Promise<APIGateway.MethodResponse> {
console.log(event); // Contains incoming request data (e.g., query params, headers and more)
const data = [
{
id: "a7b5bf50-0b5b-11e9-bc65-6bfc39f23288",
name: "some thing",
uri: `/notifications/a7b5bf50-0b5b-11e9-bc65-6bfc39f23288`
}
]
const response = {
statusCode: "200",
headers: {
},
body: JSON.stringify({
status: "ok",
data: data
})
};
return response;
};
Nice. I have upvoted your answer and removed mine. I have been using Lambda for several years now but couldn't find about this. Great work @wzr1337
– Ashan
Jan 2 at 6:45
Actually this is incorrect. The "aws-sdk" package is providing types for the AWS Client SDK and is importing the type from github.com/aws/aws-sdk-js/blob/… but this is the API Gateway control plane REST API used to create/modify an actual API in API Gateway as documented at docs.aws.amazon.com/apigateway/api-reference/resource/… . See my answer below for an alternative (and correct) way to accomplish this.
– Scott Willeke
Jan 19 at 5:05
add a comment |
After days of research I found the answer so close ;)
you return Promise<APIGateway.MethodResponse>
import { APIGateway } from "aws-sdk";
export async function readCollection (event, context, callback): Promise<APIGateway.MethodResponse> {
console.log(event); // Contains incoming request data (e.g., query params, headers and more)
const data = [
{
id: "a7b5bf50-0b5b-11e9-bc65-6bfc39f23288",
name: "some thing",
uri: `/notifications/a7b5bf50-0b5b-11e9-bc65-6bfc39f23288`
}
]
const response = {
statusCode: "200",
headers: {
},
body: JSON.stringify({
status: "ok",
data: data
})
};
return response;
};
Nice. I have upvoted your answer and removed mine. I have been using Lambda for several years now but couldn't find about this. Great work @wzr1337
– Ashan
Jan 2 at 6:45
Actually this is incorrect. The "aws-sdk" package is providing types for the AWS Client SDK and is importing the type from github.com/aws/aws-sdk-js/blob/… but this is the API Gateway control plane REST API used to create/modify an actual API in API Gateway as documented at docs.aws.amazon.com/apigateway/api-reference/resource/… . See my answer below for an alternative (and correct) way to accomplish this.
– Scott Willeke
Jan 19 at 5:05
add a comment |
After days of research I found the answer so close ;)
you return Promise<APIGateway.MethodResponse>
import { APIGateway } from "aws-sdk";
export async function readCollection (event, context, callback): Promise<APIGateway.MethodResponse> {
console.log(event); // Contains incoming request data (e.g., query params, headers and more)
const data = [
{
id: "a7b5bf50-0b5b-11e9-bc65-6bfc39f23288",
name: "some thing",
uri: `/notifications/a7b5bf50-0b5b-11e9-bc65-6bfc39f23288`
}
]
const response = {
statusCode: "200",
headers: {
},
body: JSON.stringify({
status: "ok",
data: data
})
};
return response;
};
After days of research I found the answer so close ;)
you return Promise<APIGateway.MethodResponse>
import { APIGateway } from "aws-sdk";
export async function readCollection (event, context, callback): Promise<APIGateway.MethodResponse> {
console.log(event); // Contains incoming request data (e.g., query params, headers and more)
const data = [
{
id: "a7b5bf50-0b5b-11e9-bc65-6bfc39f23288",
name: "some thing",
uri: `/notifications/a7b5bf50-0b5b-11e9-bc65-6bfc39f23288`
}
]
const response = {
statusCode: "200",
headers: {
},
body: JSON.stringify({
status: "ok",
data: data
})
};
return response;
};
answered Jan 1 at 20:19
wzr1337wzr1337
1,74931934
1,74931934
Nice. I have upvoted your answer and removed mine. I have been using Lambda for several years now but couldn't find about this. Great work @wzr1337
– Ashan
Jan 2 at 6:45
Actually this is incorrect. The "aws-sdk" package is providing types for the AWS Client SDK and is importing the type from github.com/aws/aws-sdk-js/blob/… but this is the API Gateway control plane REST API used to create/modify an actual API in API Gateway as documented at docs.aws.amazon.com/apigateway/api-reference/resource/… . See my answer below for an alternative (and correct) way to accomplish this.
– Scott Willeke
Jan 19 at 5:05
add a comment |
Nice. I have upvoted your answer and removed mine. I have been using Lambda for several years now but couldn't find about this. Great work @wzr1337
– Ashan
Jan 2 at 6:45
Actually this is incorrect. The "aws-sdk" package is providing types for the AWS Client SDK and is importing the type from github.com/aws/aws-sdk-js/blob/… but this is the API Gateway control plane REST API used to create/modify an actual API in API Gateway as documented at docs.aws.amazon.com/apigateway/api-reference/resource/… . See my answer below for an alternative (and correct) way to accomplish this.
– Scott Willeke
Jan 19 at 5:05
Nice. I have upvoted your answer and removed mine. I have been using Lambda for several years now but couldn't find about this. Great work @wzr1337
– Ashan
Jan 2 at 6:45
Nice. I have upvoted your answer and removed mine. I have been using Lambda for several years now but couldn't find about this. Great work @wzr1337
– Ashan
Jan 2 at 6:45
Actually this is incorrect. The "aws-sdk" package is providing types for the AWS Client SDK and is importing the type from github.com/aws/aws-sdk-js/blob/… but this is the API Gateway control plane REST API used to create/modify an actual API in API Gateway as documented at docs.aws.amazon.com/apigateway/api-reference/resource/… . See my answer below for an alternative (and correct) way to accomplish this.
– Scott Willeke
Jan 19 at 5:05
Actually this is incorrect. The "aws-sdk" package is providing types for the AWS Client SDK and is importing the type from github.com/aws/aws-sdk-js/blob/… but this is the API Gateway control plane REST API used to create/modify an actual API in API Gateway as documented at docs.aws.amazon.com/apigateway/api-reference/resource/… . See my answer below for an alternative (and correct) way to accomplish this.
– Scott Willeke
Jan 19 at 5:05
add a comment |
The @types/aws-lambda package provides types for using TypeScript inside of an AWS Lambda function. For AWS Lambda functions invoked using API Gateway's Lambda Proxy integration type take the following steps:
Install the package
$ yarn add --dev @types/aws-lambda
Or if you prefer npm:
$ npm i --save-dev @types/aws-lambda
Then in your handler file:
import { APIGatewayProxyEvent, Context, APIGatewayProxyResult } from "aws-lambda"
export async function hello (event: APIGatewayProxyEvent, context: Context): Promise<APIGatewayProxyResult> {
return {
statusCode: 200,
body: JSON.stringify({
message: 'Hello world',
input: event,
})
}
}
add a comment |
The @types/aws-lambda package provides types for using TypeScript inside of an AWS Lambda function. For AWS Lambda functions invoked using API Gateway's Lambda Proxy integration type take the following steps:
Install the package
$ yarn add --dev @types/aws-lambda
Or if you prefer npm:
$ npm i --save-dev @types/aws-lambda
Then in your handler file:
import { APIGatewayProxyEvent, Context, APIGatewayProxyResult } from "aws-lambda"
export async function hello (event: APIGatewayProxyEvent, context: Context): Promise<APIGatewayProxyResult> {
return {
statusCode: 200,
body: JSON.stringify({
message: 'Hello world',
input: event,
})
}
}
add a comment |
The @types/aws-lambda package provides types for using TypeScript inside of an AWS Lambda function. For AWS Lambda functions invoked using API Gateway's Lambda Proxy integration type take the following steps:
Install the package
$ yarn add --dev @types/aws-lambda
Or if you prefer npm:
$ npm i --save-dev @types/aws-lambda
Then in your handler file:
import { APIGatewayProxyEvent, Context, APIGatewayProxyResult } from "aws-lambda"
export async function hello (event: APIGatewayProxyEvent, context: Context): Promise<APIGatewayProxyResult> {
return {
statusCode: 200,
body: JSON.stringify({
message: 'Hello world',
input: event,
})
}
}
The @types/aws-lambda package provides types for using TypeScript inside of an AWS Lambda function. For AWS Lambda functions invoked using API Gateway's Lambda Proxy integration type take the following steps:
Install the package
$ yarn add --dev @types/aws-lambda
Or if you prefer npm:
$ npm i --save-dev @types/aws-lambda
Then in your handler file:
import { APIGatewayProxyEvent, Context, APIGatewayProxyResult } from "aws-lambda"
export async function hello (event: APIGatewayProxyEvent, context: Context): Promise<APIGatewayProxyResult> {
return {
statusCode: 200,
body: JSON.stringify({
message: 'Hello world',
input: event,
})
}
}
answered Jan 19 at 5:02
Scott WillekeScott Willeke
6,42712744
6,42712744
add a comment |
add a comment |
You need to return a callback otherwise if your lambda was triggered by an s3 put it will think it did not execute and will continue executing your function from time to time generating duplicated calls. You also need to return a callback from an API Gateway request so you can send a proper response to the user.
For success calls:
callback(null, response);
For error calls:
callback(error, null);
The above code works just fine returning a promise.. you do not need the callback
– wzr1337
Dec 30 '18 at 13:13
The question is: whicht Typescript type do I specify as return value of the promise
– wzr1337
Dec 30 '18 at 13:14
add a comment |
You need to return a callback otherwise if your lambda was triggered by an s3 put it will think it did not execute and will continue executing your function from time to time generating duplicated calls. You also need to return a callback from an API Gateway request so you can send a proper response to the user.
For success calls:
callback(null, response);
For error calls:
callback(error, null);
The above code works just fine returning a promise.. you do not need the callback
– wzr1337
Dec 30 '18 at 13:13
The question is: whicht Typescript type do I specify as return value of the promise
– wzr1337
Dec 30 '18 at 13:14
add a comment |
You need to return a callback otherwise if your lambda was triggered by an s3 put it will think it did not execute and will continue executing your function from time to time generating duplicated calls. You also need to return a callback from an API Gateway request so you can send a proper response to the user.
For success calls:
callback(null, response);
For error calls:
callback(error, null);
You need to return a callback otherwise if your lambda was triggered by an s3 put it will think it did not execute and will continue executing your function from time to time generating duplicated calls. You also need to return a callback from an API Gateway request so you can send a proper response to the user.
For success calls:
callback(null, response);
For error calls:
callback(error, null);
answered Dec 30 '18 at 12:39
Carlos Jafet NetoCarlos Jafet Neto
1894
1894
The above code works just fine returning a promise.. you do not need the callback
– wzr1337
Dec 30 '18 at 13:13
The question is: whicht Typescript type do I specify as return value of the promise
– wzr1337
Dec 30 '18 at 13:14
add a comment |
The above code works just fine returning a promise.. you do not need the callback
– wzr1337
Dec 30 '18 at 13:13
The question is: whicht Typescript type do I specify as return value of the promise
– wzr1337
Dec 30 '18 at 13:14
The above code works just fine returning a promise.. you do not need the callback
– wzr1337
Dec 30 '18 at 13:13
The above code works just fine returning a promise.. you do not need the callback
– wzr1337
Dec 30 '18 at 13:13
The question is: whicht Typescript type do I specify as return value of the promise
– wzr1337
Dec 30 '18 at 13:14
The question is: whicht Typescript type do I specify as return value of the promise
– wzr1337
Dec 30 '18 at 13:14
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53976371%2fwhich-type-do-i-return-with-aws-lambda-responses-in-typescript-to-suite-aws-apig%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I don't think there is an "official" types from AWS, you may find community ones. Also I think that what your looking for is API Gateway definition, as lambda can return whatever you want based on what you do with it.
– Boris Charpentier
Dec 30 '18 at 11:00
True .. IT is more 'api gateway method response type ' ;)
– wzr1337
Dec 30 '18 at 11:26