tgiardina / typescript-typeorm-realworld-example-app Goto Github PK
View Code? Open in Web Editor NEWAn boilerplate for an Express/MySQL API
An boilerplate for an Express/MySQL API
This requires three things:
This article is a good starting point. This answer should help override Express's request type to allow the canonical locals
key.
We need to add JWT authorization. Let's start with POST users
. A pull request should:
IUserDto
next to UserModel
and IUserModel
. It should insist on a token
key.toDto
method to UserModel
that returns a IUserDto
.services/user.create
to return a IUserDto
.This was mentioned as an aside in #26. Currently DTOs are located with models
(which should probably be called entitites
). Does this make sense?
This is a sub-issue of #14. It is the subject of PR #26, but I'm codifying it here. The rest of this post is copied from #26 for convenience:
Currently, (pretty much) every class has a colocated interface. This was an attempt to code to an interface, which is good if executed correctly. But I don't think it is executed correctly right now. In particular, colocating interfaces makes for both unnecessary and leaky abstractions. Move interfaces where they're used, not where they're implemented.
IUserService
next to a UserService
class. This is redundant, because UserService
already documents its interface via its implementation. Meanwhile, UserController
, which requires a user service interface, does not document it, relying instead on services
s IUserService
interface. So services
has redundant abstraction and controllers
is leaking.UserService
is based on an interface in UserControllers
. True! But it always was! We implemented UserService
to meet the requirements of UserControllers
regardless of where we placed IUserService
. The coupling was always there, it's just more explicit now.UserServices
s makes unit testing easier! Maybe if we were coding in Java 20 years ago. Sinon handles our stubbing needs just fine.According to the official specs, newbies should be able to parse our architecture in 10 minutes. There are two levers we can pull here:
I don't think we can completely solve this problem using Lever 2 alone, but ideally we can solve it without losing our core stack to Lever 1.
I think this will play our one of two ways:
A template should be (among other things) a learning tool. Why MVC? Why IoC? At the very least, we should link to articles that explain the when and how of these larger concepts.
We don't currently validate req.body, leaving it to the database to reject invalid data. This is bad practice. In particular, this means that the user service may be passed parameters that don't adhere to the appropriate interface, causing coupling and the need for weird tests (see create.invalid.test.ts
). Security is probably also an issue.
Instead, we need middleware (either via a package or coded ourselves) that handles validation before req.body reaches the controller.
This issue shows InversifyJS is dying/dead. This demo won't be very useful if it does indeed die.
In particular, it should have separate tests for running a method and testing the result. E.g.,
it('should return a correct result', async () => {
const result = await userService.create(data);
assert(result.isOk);
assert.equal(result.value.username, data.username);
assert.equal(result.value.token, data.token);
})
Should be split into
it('should run successfully', ...)
it('should have correct username', ...)
it('should have correct token', ...)
Currently, our user table only has the necessary id
and username
columns. The Real World specs also require an email
, bio
, image
, and password
columns.
A possible roadmap would be:
create-user
migration to include these columns (note: this will be a breaking change for people who already have a database set up locally).models/user
to include the new columns (both class
and interfaces
).POST /users
endpoint to require the additional fields.If interested, open a pull request with updated create-user
tests that fail with the current setup.
These are the same thing by different names.
Let's implement the GET user
endpoint as specified here. A pull request should:
refreshToken
method to services/user
that returns a IUserDto
.src/controllers/middleware/authenticate
that calls refereshToken
from 1.get
method to controllers/user
.Note: This is blocked by #10 because step 1 will use the UserModel.toDto()
method that #10 will implement.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.