Skip to content

Instantly share code, notes, and snippets.

Avatar
👋
say hi

brad oyler bradoyler

👋
say hi
View GitHub Profile
View cacheService.js
import NodeCache from 'node-cache'; // or 'redis'
class CacheService {
constructor(ttlSeconds) {
// this could also be redis
this.cache = new NodeCache({ stdTTL: ttlSeconds, checkperiod: ttlSeconds * 0.2, useClones: false });
}
get(key, storeFunction) {
@bradoyler
bradoyler / service-bus-test.js
Last active Apr 14, 2020
@azure/service-bus topic subscription test (send & receive) to confirm your subscription filters is working as expected
View service-bus-test.js
const { ServiceBusClient, ReceiveMode } = require('@azure/service-bus')
const connectionString = process.env['servicebus-connection']
async function send () {
const sbClient = ServiceBusClient.createFromConnectionString(connectionString)
const client = sbClient.createTopicClient('topic-name')
const sender = client.createSender()
// set label=TEST for use in subscription filter
const msg = { label: 'TEST', body: { foo: 'bar', ts: new Date() } }
@bradoyler
bradoyler / AppInsightsClient.ts
Last active Mar 25, 2020
Client for Azure Application Insights REST API
View AppInsightsClient.ts
import * as rp from 'request-promise-native'
function convertTable (columns: any[], rows: any[]): any[] {
return rows.map((row: any[]) => columns.reduce((obj, col, idx) => {
obj[col.name] = row[idx]
return obj
}, {}))
}
// API reference @ https://dev.applicationinsights.io/reference
@bradoyler
bradoyler / README.md
Last active Mar 24, 2020
Application Insights helper for typescript function apps
View README.md

AppInsights helper

a typescript wrapper for Application Insights

Examples

Exception tracking

import { trackException } from './appinsights-helper'

trackException({ exception: error })
View allDateConversions.js
Object.getOwnPropertyNames(Date.prototype)
.filter(name => name.startsWith('to'))
.map(method => `${method}: ${(new Date())[method]()}`)
View numberToHsl.js
function numToColor (num, maxHue = 350, minHue = 0) {
const hue = num * (maxHue - minHue) + minHue
return `hsl(${hue}, 70%, 45%)`
}
@bradoyler
bradoyler / extract-folder-git.md
Created Sep 16, 2019
extract repo from git folder
View extract-folder-git.md

git commands

git clone --mirror ssh://git@gituhub.com/user/repo.git filtered-repo
cd filtered-repo
git filter-branch --prune-empty --subdirectory-filter some/folder/path/ master
cd ..
git clone ./filtered-repo new-repo
cd new-repo
git status
@bradoyler
bradoyler / csproj-scanner.js
Last active Jul 30, 2019
scan csproj files for usage of ProjectReference
View csproj-scanner.js
const glob = require('glob')
const fs = require('fs');
const xml2js = require('xml2js');
const path = require('path');
function parseFile (filePath, parser) {
fs.readFile(filePath, (err, data) => {
parser.parseString(data, (err, result) => {
const itemgroups = result.Project.ItemGroup;
if (itemgroups && itemgroups.length) {
@bradoyler
bradoyler / README.md
Last active Mar 19, 2020
Simple Rest API client for node with support for JWT (bearer authentication) - requires Axios
View README.md

REST-client.js

Usage

const RestClient = require('./rest-client')
const client = RestClient({ baseURL: 'https://mydomain.com' })
client.setAuthHeader('my-auth-token') // optional

const { data } = await client.get('/articles')