In this article I would like to show you how you could use the @memoizeAsync decorator, from the utils-decorators library, in your application (both node and web) in one of the most elegant and simple ways.

Image for post
Image for post

Let’s say we have the following class:

export class SettingsProviderClient {  getSettings(context): Promise<SettingsDto> {
...
}
}

As you can see, the getSettings is returning a Promise of SeettingsDto. …


As I was looking for implementing a rate-limit for my NestJS server I searched in the documentation and found the corresponding page. I was really surprised to find out that there is no decorator for creating a rate-limiter for a an endpoint. I decided to create a decorator for that specific purpose and make it publicly available for others as part of the utils-decorators library.

Image for post
Image for post

In this article I will show you how you can use this decorator in your NestJS applications as well as in any other application that supports decorators.

Let’s take the following controller as a super simplified…


We will all agree that utility functions are helping us to keep a healthy code base as they are preventing us from writing the same code over and over again across our applications.

In this article I would like to introduce you to some util functions that I have created but instead of writing them as simple functions I decided to write them as decorators (If you don’t know what are decorators, I encourage you to read this article). Some of the decorators were inspired by libraries like Lodash and Underscore.

Here is the list of the available decorators with their description. …


React with Webpack and TypeScript:

I have tried to fined a starter for the above and couldn’t, so I created this snippet.

Here is package.json:

const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CheckerPlugin } = require('awesome-typescript-loader');

module.exports = () => {

return {
stats: {
assets: true,
cached: false,
cachedAssets: false,
children: false,
chunks: true,
chunkModules: false,
chunkOrigins: false,
colors: true,
depth: false,
entrypoints: false,
errors: true,
errorDetails: true,
maxModules: 0,
modules: true,
performance: false,
providedExports: false,
publicPath: true,
reasons: true,
source: false,
timings: true,
usedExports: false,
version: true,
warnings: false
},
performance: {
hints: false
},
entry: {
oogaday: require('path').resolve('./src/index.tsx'),
vendor: './src/vendor/vendor.ts',
},
output: {
path: require('path').resolve('./dist'),
filename: '[name].js',
publicPath: '/static/'
},
resolve: {
extensions: ['.js', '.ts', '.less', '.css', '.html', '.tsx'],
},
devtool: 'source-map',
module: {
rules: [
{
test: /\.ts(x?)$/,
use: ['awesome-typescript-loader']
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract(
{
fallbackLoader: 'style-loader',
loader: 'css-loader'
})
},
{
test: /\.less$/,
loader: ExtractTextPlugin.extract(
{
fallbackLoader: 'style-loader',
loader: 'css-loader!less-loader'
})
},
{
test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9=&.]+)?$/,
use: [
'file-loader?name=fonts/[name].[ext]?[hash]'
]
},
{
test: /\.(gif|png(2)?)(\?[a-z0-9=&.]+)?$/,
use: [
'file-loader?name=images/[name].[ext]?[hash]'
]
},
{
test: /\.html$/,
use: [
'html-loader'
]
}
]
},
plugins: [
new CheckerPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor', 'manifest']
}),
new ExtractTextPlugin('[name].css'),
new HtmlWebpackPlugin({
filename: 'index.html',
hash: false,
template: './src/index.html',
chunks: ['manifest', 'vendor', 'oogaday'],
inject: 'body',
chunksSortMode: 'dependency',
env: 'dev'
})
],
devServer: {
port: 3333,
inline: true,
stats: 'errors-only',
open: true,
publicPath: '/site/',
historyApiFallback: {
index: '/site/index.html' …

About

Vlad Ioffe

Father, Husband & FullStack Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store