Skip to content

Instantly share code, notes, and snippets.

View leegilmorecode's full-sized avatar
:atom:
Serverless Hero

Lee Gilmore leegilmorecode

:atom:
Serverless Hero
View GitHub Profile
@leegilmorecode
leegilmorecode / validator.js
Created July 29, 2021 09:35
A basic validate function using JSON schema and AJV
import Ajv from "ajv";
import { AppError } from "../errors/app-error";
import { errorTypes, logLevels } from "../errors/error-constants";
export function validate(obj, schema) {
const ajv = new Ajv({
allErrors: true,
});
const validator = ajv.compile(schema);
@leegilmorecode
leegilmorecode / functional.yml
Created July 29, 2021 09:25
An example of artillery functional and fuzzing tests
config:
plugins:
expect: {} # this plugin allows for assertions: https://artillery.io/docs/guides/plugins/plugin-expectations-assertions.html
fuzzer: {} # this plugin allows for fuzzing: https://artillery.io/docs/guides/plugins/plugin-fuzzer.html
ensure:
maxErrorRate: 0 # no percentage of error rate i.e. no errors or pipeline fails
payload:
path: "./data/data.csv" # pull in the employee data csv
fields:
- "id"
@leegilmorecode
leegilmorecode / load.yml
Created July 29, 2021 09:16
Example artillery load testing file
config:
plugins:
expect: {} # this plugin allows for assertions: https://artillery.io/docs/guides/plugins/plugin-expectations-assertions.html
ensure:
p95: 1000 # ensure latency is equal to or under 1000ms
maxErrorRate: 0 # no percentage of error rate i.e. no errors or pipeline fails
payload:
path: "./data/data.csv" # pull in the employee data csv
fields:
- "id"
@leegilmorecode
leegilmorecode / generate-screenshot.ts
Created July 21, 2021 06:01
The generate screenshot code
/* eslint-disable @typescript-eslint/no-var-requires */
import AWS from 'aws-sdk';
import { v4 as uuid } from 'uuid';
import { APIGatewayProxyHandler, APIGatewayEvent, APIGatewayProxyResult } from 'aws-lambda';
import { config } from './shared/config';
import { Browser, Page } from 'puppeteer-core';
const chromium = require('chrome-aws-lambda');
let browser: Browser;
let page: Page;
@leegilmorecode
leegilmorecode / get-note-types.js
Created July 15, 2021 11:54
Example function returning config values for the serverless substitutions values
function getNoteTypes(locale = "uk") {
const types = {
uk: [
{
noteTypeId: 1,
type: "Draft",
},
{
noteTypeId: 2,
type: "Published",
@leegilmorecode
leegilmorecode / get-note-types.vtl
Created July 15, 2021 11:53
VTL showing pulling through an array of objects using substitutions
{
"version": "2017-02-28",
"payload": ${noteTypes}
}
@leegilmorecode
leegilmorecode / serverless.js
Created July 15, 2021 11:50
serverless.js file showing importing values from JS files into the substitutions property
/* eslint-disable no-undef */
const { getNoteTypes } = require("./src/note-types/get-note-types");
const { getErrorTypes } = require("./src/error-types/get-error-types");
const { getErrorMessages } = require("./src/error-messages/get-error-messages");
const serverlessConfiguration = {
service: "serverless-appsync-i18n",
frameworkVersion: "2",
plugins: ["serverless-appsync-plugin"],
provider: {
@leegilmorecode
leegilmorecode / get-note-request.vtl
Created July 15, 2021 11:48
Example VTL showing substitutions and placeholders
#if($ctx.args.noteId == 0)
$utils.error($util.str.toReplace("${invalidValueErrorMessage}", "{0}", "$ctx.args.noteId"), "${invalidValueErrorType}")
#else
{
"version" : "2017-02-28",
"operation" : "GetItem",
"key" : {
"noteId" : { "S" : "${context.arguments.noteId}" },
},
"consistentRead" : true
@leegilmorecode
leegilmorecode / getErrorTypes.js
Created July 15, 2021 11:45
Basic example function which returns the correct config values based on locale
function getErrorTypes(locale = "uk") {
const errorTypes = {
uk: {
noDataErrorType: "NoData",
accessDeniedErrorType: "AccessDenied",
invalidValueErrorType: "InvalidValue",
},
fr: {
noDataErrorType: "PasDeDonnées",
accessDeniedErrorType: "AccèsRefusé",
@leegilmorecode
leegilmorecode / getErrorMessages.js
Created July 15, 2021 11:44
Basic JS function to return the write config based on locale
function getErrorMessages(locale = "uk") {
const errorMessages = {
uk: {
noDataErrorMessage: "No data",
accessDeniedErrorMessage: "Access denied",
invalidValueErrorMessage: "The value {0} is invalid",
},
fr: {
noDataErrorMessage: "Pas de données",
accessDeniedErrorMessage: "Accès refusé",