Which type do I return with AWS lambda responses in typescript to suite AWS APIGateway Method Response?












0















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?










share|improve this question

























  • 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
















0















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?










share|improve this question

























  • 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














0












0








0


2






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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












3 Answers
3






active

oldest

votes


















3














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;
};





share|improve this answer
























  • 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





















0














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,
})
}
}





share|improve this answer































    -1














    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);





    share|improve this answer
























    • 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











    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    3














    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;
    };





    share|improve this answer
























    • 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


















    3














    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;
    };





    share|improve this answer
























    • 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
















    3












    3








    3







    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;
    };





    share|improve this answer













    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;
    };






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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





















    • 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















    0














    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,
    })
    }
    }





    share|improve this answer




























      0














      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,
      })
      }
      }





      share|improve this answer


























        0












        0








        0







        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,
        })
        }
        }





        share|improve this answer













        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,
        })
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 19 at 5:02









        Scott WillekeScott Willeke

        6,42712744




        6,42712744























            -1














            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);





            share|improve this answer
























            • 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
















            -1














            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);





            share|improve this answer
























            • 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














            -1












            -1








            -1







            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);





            share|improve this answer













            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);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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



















            • 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


















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas