<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Juha-Pekka Rajaniemi on Medium]]></title>
        <description><![CDATA[Stories by Juha-Pekka Rajaniemi on Medium]]></description>
        <link>https://medium.com/@JPRajani?source=rss-a8dfe2bef507------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*R-7uNy7wBJYO3BwG.jpeg</url>
            <title>Stories by Juha-Pekka Rajaniemi on Medium</title>
            <link>https://medium.com/@JPRajani?source=rss-a8dfe2bef507------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 10:10:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@JPRajani/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Writing good looking API code for AWS Lambdas in Typescript with Kyllikki]]></title>
            <link>https://medium.com/@JPRajani/writing-good-looking-api-code-for-aws-lambdas-in-typescript-with-kyllikki-cf5a2ad34da5?source=rss-a8dfe2bef507------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf5a2ad34da5</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[serverless]]></category>
            <dc:creator><![CDATA[Juha-Pekka Rajaniemi]]></dc:creator>
            <pubDate>Fri, 28 Dec 2018 07:59:51 GMT</pubDate>
            <atom:updated>2018-12-28T07:59:51.571Z</atom:updated>
            <content:encoded><![CDATA[<p>I have been coding AWS lambdas for the last three years now. Every new project starts with a thought “This time code is going to be very well written”. And every time after few months project grows up too large to fit constraints that I wasn’t aware of. Either it’s the language I’m working (ES5 JS) or after a while, something new comes up that makes my old code look like crap or even some really weird limitations. For example how many instructions you can fit a single AWS CloudFormation file.</p><p>So I decided to create a library that supports organization of code in a way that these projects can grow up freely without concern of looming future refactoring. <a href="https://github.com/rallu/kyllikki">Kyllikki</a> is a template/framework for writing your code. It’s very easy to start a new project with a single command if you have <a href="https://serverless.com/">Serverless</a> installed. Like this:</p><pre>serverless create --template-url <a href="https://github.com/rallu/kyllikki/tree/master/template">https://github.com/rallu/kyllikki/tree/master/template</a> --path myproject</pre><p>It’s a small step up from Serverless provided <em>aws-nodejs-typescript</em> template. What does it add then? Just a small library @kyllikki/core dedicated to writing your API endpoints in a clear way with Typescript.</p><h3>What does the Kyllikki do then?</h3><p>Here is a typical class that handles API calls. GET, POST, PUT, DELETE, PATCH and ANY can be imported from core package and add added as a decorator for a function that you wish to be run. The format of a path is the same as in your <em>serverless.yml</em> file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oOn50zgHgJXWumBaC3Uavw.png" /></figure><p>As you can see I like to keep the function names very descriptive. This is also helpful as Kyllikki can use these function names as identifiers in its OpenAPI module.</p><p>And this is what you should add to the handler code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NCHtQJREeVLrtFzUoIz3zA.png" /></figure><p>With this kind of architecture, you can decide which endpoints are run by this handler. This is important for the separation or combination of log files and tracers through the AWS infrastructure. In this example, we have all Cat API endpoints running in one handler.</p><p>Currently, if you wish to have one handler per API endpoint you need to write separate classes for them. Let me know in the comments that should I do some kind of option to select functions to be run by the ApiRunner or just keep it as simple as it is.</p><h3>Advanced features</h3><p>Two other main features of Kyllikki are input validation and error handling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X9863ufSAkKSrQYCx5bvmg.png" /></figure><p>Kyllikki uses <a href="https://github.com/hapijs/joi">joi</a>-library for validations. Validations are passed to options configuration of a decorator. In this example, we are making sure that body contains JSON that has required name attribute. If this validation fails 400 error is thrown with an error message.</p><p>Other options to validate are path parameters, headers, and query string parameters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RR98tLX4Iif3sXEHXRU1vw.png" /></figure><p>Kyllikki defaults to catching errors and sending back HTTP 400 error, but it can be customized with errors parameter. Here you define errors that you are expecting to be thrown and what HTTP code should be sent. There is also a way to write a custom function to handle the error. For this check the GitHub page.</p><h3>More features!</h3><p>The initial reason why I wrote this library was that I wanted to be able to output OpenApi V3 configuration out of my code. And it still is the ultimate goal. Though I decided to separate it from the core features of Kyllikki into its own package. @kyllikki/openapi package is just for this cause. Because basics of Kyllikki are good to be used any of my future projects and openapi is not there yet.</p><p>Openapi -package is still in development. I’m not satisfied with the current method where a developer needs to write full JSON schema to document the API.</p><p>Please comment what you think about this library! It is still early work so I’m ready to change feature set quite heavily if needed.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf5a2ad34da5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>