Skip to content

Instantly share code, notes, and snippets.

View vkarpov15's full-sized avatar

Valeri Karpov vkarpov15

View GitHub Profile
@vkarpov15
vkarpov15 / archetype-20171028-custom-types.js
Last active October 28, 2017 08:16
Custom Types with Archetype
const Archetype = require('archetype');
// `Str` is a utility type for strings unrelated
// to archetype: https://www.npmjs.com/package/string
// It provides several convenient string utilities,
// including `.capitalize()`.
const Str = require('string');
const NameType = new Archetype({
// Archetype custom types are trivial: any function,
// including any class, is a viable `$type`.
@vkarpov15
vkarpov15 / archetype-20171103-conditional-required.js
Created November 3, 2017 23:46
Conditionally Required Properties with Archetype
const Archetype = require('archetype');
const CustomerType = new Archetype({
roles: {
$type: ['string'],
$enum: ['CUSTOMER', 'ADMIN'],
$required: true,
$default: ['CUSTOMER']
},
email: {
@vkarpov15
vkarpov15 / archetype-20171110-validate-config.js
Created November 11, 2017 01:05
Configuring Custom Validators for Individual Paths in Archetype
const Archetype = require('archetype');
const assert = require('assert');
// The 2nd param to the `$validate` function is the schema path,
// so `{ $type: 'number', ... }`. Can use that to configure
// validators from your schema path, like writing your own
// min/max validator
const minMax = (v, path) => {
const { $min, $max } = path;
assert.ok($min == null || v >= $min, `${v} < ${$min}`);
@vkarpov15
vkarpov15 / archetype-20171117-async-await.js
Created November 18, 2017 01:32
Async/Await with Archetype
const Archetype = require('archetype');
const UserType = new Archetype({
name: { $type: 'string' },
age: { $type: 'number' }
}).compile('UserType');
// Prints 'age: Could not cast "not a number" to number'
throwError().catch(error => console.error(error.message));
@vkarpov15
vkarpov15 / archetype-20171124-embedding.js
Created November 24, 2017 19:45
Embedding Only Certain Fields in an Embedded Archetype
const Archetype = require('archetype');
const UserType = new Archetype({
name: { $type: 'string', $required: true },
age: { $type: 'number', $required: true }
}).compile('UserType');
const RequestType = new Archetype({
createdAt: { $type: Date },
user: {

Express Gateway and Amazon API Gateway are two different products for configuring middleware servers that provide services like rate limiting, authentication, and proxying. This page will help you understand the major tradeoffs between using Amazon API Gateway and Express Gateway across several important dimensions.

Pricing

Express Gateway is an open source software project, so the software is free, but you are responsible for hosting and running your Express Gateway server. The cost of hosting an Express Gateway server may vary depending on your

@vkarpov15
vkarpov15 / archetype-20180105-transform-required.js
Created January 5, 2018 21:02
Transform an Archetype So All Properties are Not Required
const Archetype = require('archetype');
// `UserType` has 3 required properties
const UserType = new Archetype({
name: { $type: 'string', $required: true },
email: { $type: 'string', $required: true },
age: { $type: 'number', $required: true }
}).compile('UserType');
// But what if we want an `update()` function that updates a user?
@vkarpov15
vkarpov15 / archetype-20180112-virtuals.js
Created January 13, 2018 01:22
Virtual Types with Archetype
const Archetype = require('archetype');
const UserBaseType = new Archetype({
name: {
first: {
$type: 'string',
$required: true
},
last: {
$type: 'string',
@vkarpov15
vkarpov15 / archetype-20180119-store-products.js
Created January 20, 2018 02:29
Modeling Stores and Products with Archetype
const Archetype = require('archetype');
// Generic product type, because prices are specific to each store
const ProductType = new Archetype({
name: {
$type: 'string',
$required: true
}
}).compile('ProductType');
@vkarpov15
vkarpov15 / promise1.js
Created April 5, 2018 12:38
Write Your Own Node.js Promise Library from Scratch, Part 1
class MyPromise {
constructor(executor) {
if (typeof executor !== 'function') {
throw new Error('Executor must be a function');
}
// Internal state. `$state` is the state of the promise, and `$chained` is
// an array of the functions we need to call once this promise is settled.
this.$state = 'PENDING';
this.$chained = [];