Azure functions TypeScript decorator Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!What is TypeScript and why would I use it in place of JavaScript?get and set in TypeScriptAre strongly-typed functions as parameters possible in TypeScript?TypeScript Converting a String to a numberHow to implement a typescript decorator?typescript getting error TS2304: cannot find name ' require'Typescript: Interfaces vs TypesUsing In-Proc COM DLL with Azure FunctionNode typescript global service to return value from a functionchain operator function in typescript

What is a more techy Technical Writer job title that isn't cutesy or confusing?

How to achieve cat-like agility?

Problem with display of presentation

How do Java 8 default methods hеlp with lambdas?

How do I find my Spellcasting Ability for my D&D character?

Keep at all times, the minus sign above aligned with minus sign below

Diophantine equation 3^a+1=3^b+5^c

.bashrc alias for a command with fixed second parameter

Determine whether an integer is a palindrome

Besides transaction validation, are there any other uses of the Script language in Bitcoin

Vertical ranges of Column Plots in 12

How to make triangles with rounded sides and corners? (squircle with 3 sides)

Is this Half-dragon Quaggoth boss monster balanced?

How does the body cool itself in a stillsuit?

Sally's older brother

What are some likely causes to domain member PC losing contact to domain controller?

Did pre-Columbian Americans know the spherical shape of the Earth?

NIntegrate on a solution of a matrix ODE

Why did Bronn offer to be Tyrion Lannister's champion in trial by combat?

Noise in Eigenvalues plot

The Nth Gryphon Number

Is there a verb for listening stealthily?

Random body shuffle every night—can we still function?

calculator's angle answer for trig ratios that can work in more than 1 quadrant on the unit circle



Azure functions TypeScript decorator



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!What is TypeScript and why would I use it in place of JavaScript?get and set in TypeScriptAre strongly-typed functions as parameters possible in TypeScript?TypeScript Converting a String to a numberHow to implement a typescript decorator?typescript getting error TS2304: cannot find name ' require'Typescript: Interfaces vs TypesUsing In-Proc COM DLL with Azure FunctionNode typescript global service to return value from a functionchain operator function in typescript



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Recently Azure functions released support for TypeScript:



import AzureFunction, Context, HttpRequest from '@azure/functions';

@some_decorator - ???
const httpTrigger: AzureFunction = async function (context: Context,
req: HttpRequest): Promise<void>



export default httpTrigger;


I'm looking for a good approach to implement a pre-function call.
For instance, the pre-function could do authorization checks or whatever else is necessary before the function in question is executed.



I'm wondering which TypeScript decorators would be the best and cleanest option but not sure about the implementation.










share|improve this question






























    0















    Recently Azure functions released support for TypeScript:



    import AzureFunction, Context, HttpRequest from '@azure/functions';

    @some_decorator - ???
    const httpTrigger: AzureFunction = async function (context: Context,
    req: HttpRequest): Promise<void>



    export default httpTrigger;


    I'm looking for a good approach to implement a pre-function call.
    For instance, the pre-function could do authorization checks or whatever else is necessary before the function in question is executed.



    I'm wondering which TypeScript decorators would be the best and cleanest option but not sure about the implementation.










    share|improve this question


























      0












      0








      0








      Recently Azure functions released support for TypeScript:



      import AzureFunction, Context, HttpRequest from '@azure/functions';

      @some_decorator - ???
      const httpTrigger: AzureFunction = async function (context: Context,
      req: HttpRequest): Promise<void>



      export default httpTrigger;


      I'm looking for a good approach to implement a pre-function call.
      For instance, the pre-function could do authorization checks or whatever else is necessary before the function in question is executed.



      I'm wondering which TypeScript decorators would be the best and cleanest option but not sure about the implementation.










      share|improve this question
















      Recently Azure functions released support for TypeScript:



      import AzureFunction, Context, HttpRequest from '@azure/functions';

      @some_decorator - ???
      const httpTrigger: AzureFunction = async function (context: Context,
      req: HttpRequest): Promise<void>



      export default httpTrigger;


      I'm looking for a good approach to implement a pre-function call.
      For instance, the pre-function could do authorization checks or whatever else is necessary before the function in question is executed.



      I'm wondering which TypeScript decorators would be the best and cleanest option but not sure about the implementation.







      node.js typescript azure azure-functions






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 22 at 14:08









      Dexter

      2,07231726




      2,07231726










      asked Mar 22 at 12:42









      PawelPawel

      1




      1






















          1 Answer
          1






          active

          oldest

          votes


















          0














          That's a great question!



          Decorators are basically functions that wrap the function on which they are applied on. You can read more about how they work in their official doc.



          Now coming to Azure Functions, typescript functions are actually compiles to JavaScript and are then run. So, decorators will work out of the box. :)




          Note that you will have to set the experimentalDecorators flag in your tsconfig.json file to true for this to work as mentioned in the docs




          Here is simple sample of how you could implement your own custom decorator



          import AzureFunction, Context, HttpRequest from '@azure/functions';

          function checkUserId(userId: string)
          return function(
          target: Object,
          propertyKey: string,
          descriptor: PropertyDescriptor
          )
          const originalValue = descriptor.value;

          descriptor.value = async function(...args: any[])
          const context: Context = args[0];
          const req: HttpRequest = args[1];

          if (req.headers['x-func-user-id'] !== userId)
          context.res =
          status: 403,
          body: 'User not authorized!'
          ;
          return;


          // Call the original function
          await originalValue.apply(this, args);
          return;
          ;
          ;


          class HttpTrigger
          @checkUserId('azure-user')
          static async function(context: Context, req: HttpRequest): Promise<void> (req.body && req.body.name);

          if (name)
          context.res = ;
          else
          context.res =
          status: 400,
          body: 'Please pass a name on the query string or in the request body'
          ;




          export default HttpTrigger.function;


          And you can test this function like this



          curl --request GET 
          --url 'http://localhost:7071/api/HttpTrigger?name=Azure'
          --header 'x-func-user-id: azure-user'





          share|improve this answer























          • Your solution looks great! I will test it later and get back with the feedback, thank you a lot!

            – Pawel
            Apr 2 at 9:00











          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%2f55299880%2fazure-functions-typescript-decorator%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          That's a great question!



          Decorators are basically functions that wrap the function on which they are applied on. You can read more about how they work in their official doc.



          Now coming to Azure Functions, typescript functions are actually compiles to JavaScript and are then run. So, decorators will work out of the box. :)




          Note that you will have to set the experimentalDecorators flag in your tsconfig.json file to true for this to work as mentioned in the docs




          Here is simple sample of how you could implement your own custom decorator



          import AzureFunction, Context, HttpRequest from '@azure/functions';

          function checkUserId(userId: string)
          return function(
          target: Object,
          propertyKey: string,
          descriptor: PropertyDescriptor
          )
          const originalValue = descriptor.value;

          descriptor.value = async function(...args: any[])
          const context: Context = args[0];
          const req: HttpRequest = args[1];

          if (req.headers['x-func-user-id'] !== userId)
          context.res =
          status: 403,
          body: 'User not authorized!'
          ;
          return;


          // Call the original function
          await originalValue.apply(this, args);
          return;
          ;
          ;


          class HttpTrigger
          @checkUserId('azure-user')
          static async function(context: Context, req: HttpRequest): Promise<void> (req.body && req.body.name);

          if (name)
          context.res = ;
          else
          context.res =
          status: 400,
          body: 'Please pass a name on the query string or in the request body'
          ;




          export default HttpTrigger.function;


          And you can test this function like this



          curl --request GET 
          --url 'http://localhost:7071/api/HttpTrigger?name=Azure'
          --header 'x-func-user-id: azure-user'





          share|improve this answer























          • Your solution looks great! I will test it later and get back with the feedback, thank you a lot!

            – Pawel
            Apr 2 at 9:00















          0














          That's a great question!



          Decorators are basically functions that wrap the function on which they are applied on. You can read more about how they work in their official doc.



          Now coming to Azure Functions, typescript functions are actually compiles to JavaScript and are then run. So, decorators will work out of the box. :)




          Note that you will have to set the experimentalDecorators flag in your tsconfig.json file to true for this to work as mentioned in the docs




          Here is simple sample of how you could implement your own custom decorator



          import AzureFunction, Context, HttpRequest from '@azure/functions';

          function checkUserId(userId: string)
          return function(
          target: Object,
          propertyKey: string,
          descriptor: PropertyDescriptor
          )
          const originalValue = descriptor.value;

          descriptor.value = async function(...args: any[])
          const context: Context = args[0];
          const req: HttpRequest = args[1];

          if (req.headers['x-func-user-id'] !== userId)
          context.res =
          status: 403,
          body: 'User not authorized!'
          ;
          return;


          // Call the original function
          await originalValue.apply(this, args);
          return;
          ;
          ;


          class HttpTrigger
          @checkUserId('azure-user')
          static async function(context: Context, req: HttpRequest): Promise<void> (req.body && req.body.name);

          if (name)
          context.res = ;
          else
          context.res =
          status: 400,
          body: 'Please pass a name on the query string or in the request body'
          ;




          export default HttpTrigger.function;


          And you can test this function like this



          curl --request GET 
          --url 'http://localhost:7071/api/HttpTrigger?name=Azure'
          --header 'x-func-user-id: azure-user'





          share|improve this answer























          • Your solution looks great! I will test it later and get back with the feedback, thank you a lot!

            – Pawel
            Apr 2 at 9:00













          0












          0








          0







          That's a great question!



          Decorators are basically functions that wrap the function on which they are applied on. You can read more about how they work in their official doc.



          Now coming to Azure Functions, typescript functions are actually compiles to JavaScript and are then run. So, decorators will work out of the box. :)




          Note that you will have to set the experimentalDecorators flag in your tsconfig.json file to true for this to work as mentioned in the docs




          Here is simple sample of how you could implement your own custom decorator



          import AzureFunction, Context, HttpRequest from '@azure/functions';

          function checkUserId(userId: string)
          return function(
          target: Object,
          propertyKey: string,
          descriptor: PropertyDescriptor
          )
          const originalValue = descriptor.value;

          descriptor.value = async function(...args: any[])
          const context: Context = args[0];
          const req: HttpRequest = args[1];

          if (req.headers['x-func-user-id'] !== userId)
          context.res =
          status: 403,
          body: 'User not authorized!'
          ;
          return;


          // Call the original function
          await originalValue.apply(this, args);
          return;
          ;
          ;


          class HttpTrigger
          @checkUserId('azure-user')
          static async function(context: Context, req: HttpRequest): Promise<void> (req.body && req.body.name);

          if (name)
          context.res = ;
          else
          context.res =
          status: 400,
          body: 'Please pass a name on the query string or in the request body'
          ;




          export default HttpTrigger.function;


          And you can test this function like this



          curl --request GET 
          --url 'http://localhost:7071/api/HttpTrigger?name=Azure'
          --header 'x-func-user-id: azure-user'





          share|improve this answer













          That's a great question!



          Decorators are basically functions that wrap the function on which they are applied on. You can read more about how they work in their official doc.



          Now coming to Azure Functions, typescript functions are actually compiles to JavaScript and are then run. So, decorators will work out of the box. :)




          Note that you will have to set the experimentalDecorators flag in your tsconfig.json file to true for this to work as mentioned in the docs




          Here is simple sample of how you could implement your own custom decorator



          import AzureFunction, Context, HttpRequest from '@azure/functions';

          function checkUserId(userId: string)
          return function(
          target: Object,
          propertyKey: string,
          descriptor: PropertyDescriptor
          )
          const originalValue = descriptor.value;

          descriptor.value = async function(...args: any[])
          const context: Context = args[0];
          const req: HttpRequest = args[1];

          if (req.headers['x-func-user-id'] !== userId)
          context.res =
          status: 403,
          body: 'User not authorized!'
          ;
          return;


          // Call the original function
          await originalValue.apply(this, args);
          return;
          ;
          ;


          class HttpTrigger
          @checkUserId('azure-user')
          static async function(context: Context, req: HttpRequest): Promise<void> (req.body && req.body.name);

          if (name)
          context.res = ;
          else
          context.res =
          status: 400,
          body: 'Please pass a name on the query string or in the request body'
          ;




          export default HttpTrigger.function;


          And you can test this function like this



          curl --request GET 
          --url 'http://localhost:7071/api/HttpTrigger?name=Azure'
          --header 'x-func-user-id: azure-user'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 26 at 17:15









          PramodValavala-MSFTPramodValavala-MSFT

          53114




          53114












          • Your solution looks great! I will test it later and get back with the feedback, thank you a lot!

            – Pawel
            Apr 2 at 9:00

















          • Your solution looks great! I will test it later and get back with the feedback, thank you a lot!

            – Pawel
            Apr 2 at 9:00
















          Your solution looks great! I will test it later and get back with the feedback, thank you a lot!

          – Pawel
          Apr 2 at 9:00





          Your solution looks great! I will test it later and get back with the feedback, thank you a lot!

          – Pawel
          Apr 2 at 9:00



















          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%2f55299880%2fazure-functions-typescript-decorator%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript