Skip to content

Instantly share code, notes, and snippets.

View Goldziher's full-sized avatar
🐙
Codoctupus

Na'aman Hirschfeld Goldziher

🐙
Codoctupus
  • Philipps & Byrne GmbH
  • Berlin
  • 23:17 (UTC +02:00)
  • LinkedIn in/nhirschfeld
View GitHub Profile
@Goldziher
Goldziher / generate_api_types.sh
Last active November 30, 2021 21:47
Script to generate api specs from a given api using DTSGenerator
#!/bin/bash
SPECS_DOWNLOAD_URL="https://url-of-api.com/specs"
FILE="$PWD/src/my-api-specs.ts"
if [[ -f "$FILE" ]]; then
rm "$FILE"
fi
# use dtsgen to generate the target typescript file
dtsgen --url $SPECS_DOWNLOAD_URL >> "$FILE"
@Goldziher
Goldziher / recursiveResolve.ts
Last active November 27, 2021 19:21
Recursive Promise Resolve
function isRecord<T = any>(value: unknown): value is Record<string | symbol, T> {
return !!(value && typeof value === 'object');
}
function isPromise<T = unknown>(value: unknown): value is Promise<T> {
return isRecord(value) && Reflect.has(value, 'then');
}
async function recursiveResolve<T>(
parsedObject: Record<string, any>,
@Goldziher
Goldziher / duck.ts
Last active November 27, 2021 16:50
Duck typing in typescript
interface Duck {
quack(): string;
}
function isDuck(value: unknown): value is Duck {
return !!(
value &&
typeof value === 'object' &&
typeof Reflect.get(value, 'quack') === 'function'
);
@Goldziher
Goldziher / duck.js
Last active November 27, 2021 16:50
Duck typing in javascript
function isDuck(value) {
return !!(
value &&
typeof value === 'object' &&
typeof Reflect.get(value, 'quack') === 'function'
);
}
@Goldziher
Goldziher / duck.py
Last active November 27, 2021 18:52
python duck typing example
from typing import Any
def is_duck(value: Any) -> bool:
try:
value.quack()
return True
except (Attribute, ValueError):
return False
@Goldziher
Goldziher / chunk.py
Created November 10, 2021 13:32
generic python chunk function
from itertools import islice
from typing import Iterator, Sequence, TypeVar
T = TypeVar("T")
def chunk(sequence: Sequence[T], size: int) -> Iterator[list[T]]:
"""given a sequence, return an iterator of lists of the given size"""
return iter(lambda: list(islice(iter(sequence), size)), [])
@Goldziher
Goldziher / Dockerfile
Last active February 10, 2023 08:41
fastAPI Poetry Docker image
FROM python:3.9-slim AS install
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y --no-install-recommends curl \
&& apt-get autoremove -y
RUN pip install --upgrade pip
WORKDIR /app/
# install poetry and keep the get-poetry script so it can be reused later.
ENV POETRY_HOME="/opt/poetry"
RUN curl https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py > get-poetry.py
@Goldziher
Goldziher / useInterval.ts
Created October 21, 2021 19:27
React useInterval hook
import { useEffect, useRef } from 'react';
export function useInterval(
callback: (clearInterval: () => void) => any,
delay: number | null,
): () => void {
const idRef = useRef<null | number>(null);
const clearInterval = () => {
if (idRef.current) {
@Goldziher
Goldziher / UserProfile.story.ts
Created August 1, 2021 06:49
Example usage of Interface Forge factory in storybook
import { Meta } from '@storybook/react';
import { UserDTO } from '@shared/dto/User.dto';
import { UserDTOFactory } from '@shared/factories/User.dto.factory';
import React from 'react';
import UserProfile from '@frontend/pages/user-profile';
export default {
title: 'Users/Profile',
component: UserProfile,
} as Meta;
@Goldziher
Goldziher / backend-api.service.ts
Created August 1, 2021 06:48
Service method returning mock data
// ...
getUser = async (id: string): Promise<UserDTO> => {
const { data } = await UserDTOFactory.build({ id });
return data;
};