Skip to content

Instantly share code, notes, and snippets.

View dupski's full-sized avatar

Russell Briggs dupski

  • United Kingdom
View GitHub Profile
@dupski
dupski / models_with_api.ts
Last active May 19, 2018 23:26
Create a GraphQL API in 5 minutes - API
import { ModelApiManager } from 'rev-api';
// Register model classes with an API Manager
export const api = new ModelApiManager(modelManager);
api.register(User, { operations: ['read', 'create'] })
api.register(Post, { operations: ['read', 'create', 'update', 'remove'] })
api.register(Comment, { operations: ['read', 'create', 'update', 'remove'] })
@dupski
dupski / models.ts
Last active May 19, 2018 23:03
Create a GraphQL in 5 minutes - models.ts
import * as rev from 'rev-models';
export class User {
@rev.AutoNumberField({ primaryKey: true })
id: number;
@rev.EmailField()
email: string;
@rev.TextField()
full_name: string;
@rev.RelatedModelList({ model: 'Post', field: 'user' })
@dupski
dupski / rev_modelmanager.ts
Last active March 24, 2018 01:22
RevJS Example - using a ModelManager
import { ModelManager, InMemoryBackend } from 'rev-models';
import * as models from '../models';
// Create a ModelManager with an in-memory storage backend (for development)
export const manager = new ModelManager();
manager.registerBackend('default', new InMemoryBackend());
manager.register(models.Post);
manager.register(models.User);
@dupski
dupski / docgen.ts
Created February 19, 2018 08:05
Code to extract type info from a typescript project
// Some bits from here:
// https://github.com/YousefED/typescript-json-schema/blob/master/typescript-json-schema.ts
// and here:
// https://github.com/dtinth/a-javascript-and-typescript-documentation-generator-based-on-typescript-compiler/blob/master/src/generator/createWalker.ts
import * as ts from 'typescript';
import * as path from 'path'
export function programFromConfig(configFileName: string): ts.Program {
@dupski
dupski / server.ts
Last active September 9, 2017 00:10
Koa & Typescript with middleware
import * as Koa from 'koa';
import * as Router from 'koa-router';
const app = new Koa();
app.use(async (ctx, next) => {
// Log the request to the console
console.log('Url:', ctx.url);
// Pass the request to the next middleware function
await next();
@dupski
dupski / server.ts
Last active September 8, 2017 23:35
Koa & Typescript - server.ts
import * as Koa from 'koa';
import * as Router from 'koa-router';
const app = new Koa();
const router = new Router();
router.get('/*', async (ctx) => {
ctx.body = 'Hello World!';
});
@dupski
dupski / integration-test-example.ts
Last active July 18, 2017 12:02
InversifyJS Integration Test Example
describe('/apps integration tests', () => {
let testServer: Hapi.Server;
let successResponse: Hapi.InjectedResponseObject;
let failResponse: Hapi.InjectedResponseObject;
beforeAll(async () => {
testServer = await createTestServer();
container.rebind(TYPES.ILogger).toConstantValue(new MockLogger());
@dupski
dupski / routes-example.ts
Last active July 18, 2017 10:45
InversifyJS Route Config Example
import { container } from '../ioc/inversify.config';
import { TYPES } from '../ioc/types';
import { IMyAppsController } from '../controllers/MyAppsController';
const server = new Hapi.Server();
server.route({
method: 'GET',
path: '/apps',
handler: (request, reply) => {
@dupski
dupski / controller-example.tsx
Last active July 18, 2017 10:03
InversifyJS Controller Example
@injectable()
class MyAppsController {
@inject(TYPES.ILogger) private _logger: ILogger;
@inject(TYPES.IReactRenderer) private _react: IReactRenderer;
@inject(TYPES.IApplicationService) private _appService: IApplicationService;
myAppsList(request: Hapi.Request, reply: Hapi.Reply) {
if (isNaN(request.query.userId)) {
import request from 'axios';
Promise.all([
request({ url: 'https://randomuser.me/api/', responseType: 'json' }),
request({ url: 'http://quotes.rest/qod.json', responseType: 'json' })
])
.then((responses) => {
let user = responses[0].data.results[0];
let quote = responses[1].data.contents.quotes[0];
console.log(user.name.first + ' says: ' + quote.quote);