DEV Community

Cover image for Write a small API using Deno

Write a small API using Deno

Kryz on November 12, 2019

In this post, I will show you how to create a small API using Deno - the newest runtime to run Javascript and Typescript, created by the author of ...
Collapse
 
dels07 profile image
Deli Soetiawan • Edited

I'm a bit of mixed that Deno doesn't have some kind of package manager, if you import module from master your code will break so soon... if you import using version number you must replace it in all places when you want to update the dependency.

I remember that golang decided to implement dep & modules due former problem.

I think more and more people with use some kind of workaround like this:
github.com/crookse/deno-drash/blob...
github.com/oakserver/oak/blob/mast...

Collapse
 
kryz profile image
Kryz • Edited

Hello Deli,
thank you for your comment. I understand your doubts. In my examples I import dependencies directly from master for simplicity, but there is a solution for the problem you described:

Step 1. Import a specific version instead of master (don't forget to add "v" before the version number):

import { v4 as uuid } from "https://deno.land/std@v0.22.0/uuid/mod.ts";

Step 2. Put this import and all external dependencies into a separate file and re-export them (change "import" from the code above to "export"):

imports.ts

export { v4 as uuid } from "https://deno.land/std@v0.22.0/uuid/mod.ts";

3. Import from imports.ts and not directly from the internet:

import { uuid } from "../imports.ts";

Advantages:

  • Easy management - all your external dependencies are listed in one file
  • You don't need to update many files when you update the version of your dependency
  • You can simply replace an external implementation, but still use the same module name (because of "as uuid")
Collapse
 
opensas profile image
opensas

great answer, I think it should be added to the article

Collapse
 
dels07 profile image
Deli Soetiawan • Edited

I already know your method beforehand, but it would be great if there's cli tool to manage deps.ts to keep things standard.

Thread Thread
 
kryz profile image
Kryz

I think a separate tool like npm won't be added because as the deno docs state: "Deno explicitly takes on the role of both runtime and package manager"

Another solution supported by Deno are file maps: [deno.land/std/manual.md#import-maps]

Collapse
 
steveblue profile image
Stephen Belovarich • Edited

Easy management from the perspective of the person authoring the code, but how is this easily manageable from the perspective of a monorepo where dependencies need to be updated en masse?

Collapse
 
mikevb3 profile image
Miguel Villarreal

im yet to test it, but you can make a import map.

deno.land/std/manual.md#import-maps

Collapse
 
texteditords profile image
Denis Sirbu • Edited

Hi Kryz! The services/createId.ts wasn't working for me I added a function call after uuid

export default () => uuid.generate();

this resolved the sittuation, is this right what I did?

Regards, Denis

Collapse
 
kryz profile image
Kryz

Hello,
yes, thank you! I updated the source code and my example.

Collapse
 
texteditords profile image
Denis Sirbu

Thank you for the article was very interesting!

Regards, Denis.

Collapse
 
sholladay profile image
Seth Holladay

If you need a web server framework for Deno, please give Pogo a try. It is well documented and tested.
github.com/sholladay/pogo

Collapse
 
dharmaram profile image
dharmaram

TS7031 [ERROR]: Binding element 'response' implicitly has an 'any' type.
export default async ({ params, request, response }) => {
~~~~~~~~
at file:///C:/Users/DHARM/deno-js/deno-practice/handlers/updateUser.ts:3:42

Collapse
 
anditsou profile image
Ítalo Sousa

I had the same issue. It's an issue that happens after newer updates at Deno's TS compiler. You should explicitly type those variables. I did this way and it worked for me:

import { Response } from 'https://deno.land/x/oak/mod.ts';

const hello = ({ response }: { response: Response}) => {
    response.body = { message: 'Hello World' };
};

export default {
    hello
}
Collapse
 
ryanlsmith4 profile image
Ryan Smith

So in march of 2020 this tutorial no longer works on mac catalina I get 15 errors first starting with
error TS7031: Binding element 'request' implicitly has an 'any' type.

► file:///~/dev/personalProject/denoStarterApi/handlers/createUser.ts:3:25

3 export default async ({ request, response }) => {

Can we update tutorial or maybe I'm just wrong? literally followed verbatim though

Collapse
 
lexiebkm profile image
Alexander B.K.

Thanks for this article.

I like its use of ES6 module. I want to give it a try, at least for learning.

But I see the following statement in its site :
"A word of caution: Deno is very much under development.
We encourage brave early adopters, but expect bugs large and small."
So we can expect you become one of those brave early adopters. :)

I am waiting for its stable release. For now, I want to enjoy using Node, although I am still relatively new to this javascript web server/run time platform. Currently for my real project, I use PHP + Laravel.

Collapse
 
opensas profile image
opensas

Excellent tutorial, thanks a lot. It looks like deno web framework are somehow in it's infancy stage (which is logical, of course). It seems like there are still many missing pieces (db drivers, orms, testing frameworks, etc...)
I wonder if any of the full-featured framework's authors are working on a deno port (feathers, nestjs, foalts, etc...)

Collapse
 
hshifan profile image
Shifan Hassan

Thank you for the great little tutorial on deno, I managed to complete it and its working great.

Collapse
 
seanmclem profile image
Seanmclem

Is there a decent HTTP Library yet?

 
seanmclem profile image
Seanmclem

Thanks, I'll take a closer look.

Collapse
 
jacobmgevans profile image
Jacob Evans

I truly hope this Anagram pattern continues as an ecosystem trope for Deno (Node) Koa (Oak), it's hilarious.

Great article it's helping me to grok and implement some of my own Deno mini-projects :)

Collapse
 
fed135 profile image
Frederic Charette

I'd be interested to see some benchmarks comparing this API with an equivalent one in Node.

So that we can see what's the latency, throughput and resource usage differences.

Collapse
 
johncarroll profile image
John Carroll
Collapse
 
fed135 profile image
Frederic Charette • Edited

I've been visiting that page every time a new minor comes out. I'd love to see some high-level implementation performance comparisons against modern node versions; the current benchmarks pit deno against Node 8

Collapse
 
seanmclem profile image
Seanmclem • Edited

I was referring more to handling HTTP requests. I don't know much about Deno, but I had read it was lacking that. Maybe that was old information