Skip to content

Instantly share code, notes, and snippets.

View acomagu's full-sized avatar

Yuki Ito acomagu

  • The Designium, Inc.
  • Musashimurayama-shi, Tokyo, JAPAN
  • X @acomagu_u
View GitHub Profile
@acomagu
acomagu / cred.ts
Last active December 5, 2023 09:06
The Demo of Stripe Embedded Checkout
import * as toml from 'https://deno.land/std@0.207.0/toml/mod.ts';
async function getStripeConfig() {
const { stdout } = await new Deno.Command('stripe', {
args: ['config', '--list'],
stderr: 'inherit',
}).output();
return toml.parse(new TextDecoder().decode(stdout)) as any;
}
@acomagu
acomagu / memoize.ts
Created October 28, 2023 09:22
TypeScript generic recursive memoization
function memoize<Args extends unknown[], R>(f: (...args: Args) => R): (...args: Args) => R {
const cache = new Map();
let retCache: { value: R } | undefined;
return (...args: Args): R => {
if (args.length > 0) {
if (!cache.has(args[0])) {
cache.set(args[0], memoize((...restArgs) => (f as any)(args[0], ...restArgs)));
}
return cache.get(args[0])(...args.slice(1));
}
@acomagu
acomagu / gist:ebe883e861a36e3e95b0180e6e7c3b19
Created September 30, 2023 08:51
Get Schema SDL from Remote GraphQL API
import graphql from 'graphql';
const resp = await fetch(url, { method: 'POST', body: JSON.stringify({ query: graphql.getIntrospectionQuery() }), headers: { 'content-type': 'application/json' } });
const introspectionResult = await resp.json();
const schema = graphql.buildClientSchema(introspectionResult.data);
const sdl = graphql.printSchema(schema);
import { list } from '@keystone-6/core';
import { graphql } from '@keystone-6/core';
import { allowAll } from '@keystone-6/core/access';
import { checkbox, relationship, text, timestamp } from '@keystone-6/core/fields';
import { select } from '@keystone-6/core/fields';
import { BaseListTypeInfo, CommonFieldConfig, FieldTypeFunc, fieldType } from '@keystone-6/core/types';
interface TranslatedFieldConfig<
ListTypeInfo extends BaseListTypeInfo,
LanguageTag extends string,
@acomagu
acomagu / index.js
Created February 16, 2023 10:34
JavaScript: Log all HTTP request/response and patch console.log to send log to external API
const fetch = require('node-fetch');
const util = require('util');
util.inspect.defaultOptions.depth = null;
console.log = (...args) => {
return fetch('https://webhook.site/XXXXX', {
method: 'POST',
body: util.format(...args),
});
};
@acomagu
acomagu / auth-directive.ts
Created February 5, 2022 11:40
@auth(role: ...) GraphQL directive implementation in TypeScript.
import { mapSchema, getDirective, MapperKind } from '@graphql-tools/utils';
import { defaultFieldResolver, GraphQLError, GraphQLFieldResolver, GraphQLSchema } from 'graphql';
function wrapField<Field extends { resolve?: GraphQLFieldResolver<any, any> }>(field: Field, authDirective: Record<string, any>) {
const { resolve = defaultFieldResolver } = field;
field.resolve = (src, args, ctx, info) => {
if (ctx.role !== authDirective.role) throw new GraphQLError(`${authDirective.role} role is needed but your role is ${ctx.role}.`);
return resolve(src, args, ctx, info);
};
@acomagu
acomagu / encrypt-with-github-key.sh
Last active March 31, 2021 09:40
Script to encrypt/decrypt short message with the SSH public key on GitHub.
echo abc | openssl rsautl -encrypt -pubin -inkey <(ssh-keygen -f <(curl https://github.com/acomagu.keys) -e -m PKCS8) | openssl rsautl -decrypt -inkey ~/.ssh/id_rsa
<!DOCTYPE html>
<body>
<form method="POST" action="https://raraya:Qm3VMV7pgnAgNch2WzqWYN5kDVNLGmiNCk6nihwc8U4KwEKSmd@shop-stage.raraya.co.jp/external/ordermadeparts.php">
<input type="hidden" name="apiKey" value="ZWJcQYnhM9YNS4OiVcphyDmEtNGr8rKv4qgEUKdDxGZ1WEzBnc" />
<input type="hidden" name="a" value="1" />
<input type="hidden" name="b" value="1" />
<input type="hidden" name="c" value="1" />
<input type="hidden" name="d" value="1" />
<input type="hidden" name="e" value="1" />
<input type="hidden" name="f" value="1" />
const { Node, Construct } = require('constructs');
class RootConstruct extends Construct {
onSynthesize() {
console.log(this.synthesize());
}
synthesize() {
return Object.assign({},
...Node.of(this).children.map(child => child.synthesize()),
);
@acomagu
acomagu / xml_marshallable_map.go
Created January 4, 2021 02:10
The map[string]interface implementing XMLMarshaller
package xml_marshallable_map
// XMLMarshallableMap contains nestable data and implements
// XMLMarshaller. The actual type of interface{} part must be string or
// map[string]interface{}. The fields are sorted in alphabetical
// order and encoded.
type XMLMarshallableMap map[string]interface{}
func (m XMLMarshallableMap) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if err := e.EncodeToken(start); err != nil {