Skip to content

Instantly share code, notes, and snippets.

View marcolink's full-sized avatar
🪐

Marco Link marcolink

🪐
View GitHub Profile
@marcolink
marcolink / contentful-fetch-example.md
Last active July 28, 2023 07:51
Use contentful sdk/library with fetch adapter
@marcolink
marcolink / axios-migration-guide_0.xx_to_1.x.md
Last active July 18, 2023 14:09
axios migration guide 0.xx to 1.x

axios migration guide 0.x to 1.x

at the time writing this, the latest version was 1.4.0.

Resources

Request Interceptors

The provided type for request intercetptor functions changed:

@marcolink
marcolink / useAllTrue.ts
Created August 4, 2022 07:29
React hook for a reduced boolean value
import { useEffect, useState } from "react";
function reduce(record: Record<string, boolean>): boolean {
return Object.keys(record).reduce((previousValue, currentValue) => {
if (!record[currentValue]) {
return false;
}
return previousValue;
}, true);
}
@marcolink
marcolink / fetchContentfulCollection.ts
Created October 7, 2021 09:30
fetch contentful collection
import { CollectionProp } from 'contentful-management/types';
type CollectionFuncResponse<TItem> = Promise<CollectionProp<TItem>>;
type CollectionFunc<TItem> = (skip: number, limit: number) => CollectionFuncResponse<TItem>;
export async function getAllCollectionItems<TItem>(
endpoint: CollectionFunc<TItem>,
skip = 0,
limit = 100
): Promise<Array<TItem>> {
@marcolink
marcolink / chunkify.js
Last active January 17, 2023 23:46
Create contentful export chunks
/*
For very big spaces, we can create chunks of the export, and import them separately.
To make it work, we have to keep a specific order.
*/
const fs = require('fs');
const path = require('path');
/* the order of types must be kept */
const types = [
@marcolink
marcolink / LocalizedEntry.ts
Created April 1, 2021 07:32
Helper for localized (contentful) Entry type
/*
* https://github.com/contentful/contentful.js/blob/master/index.d.ts#L81
*/
type Entry<Fields> = {
sys: {
id: string
}
fields : Fields
}
@marcolink
marcolink / get-override-static.ts
Last active March 26, 2021 23:32
Get override for static field in typescript class
/*
https://www.typescriptlang.org/play?ssl=1&ssc=1&pln=16&pc=15#code/MYGwhgzhAECC0G8BQ1oAcCuAjEBLY0EALmEftAGa4CmIAJtALyIB2YAttQFzQDksvAL4p02PASIB7AMpEATrhYBzABQBKRCNRzqRDHJbQA8lgBW1YEQB0S3QAU5kqUQCeaakYoqiAC1wQ1K2BJFmI5DEtJOSsqWjoRYWEkUEgYACFoagAPImoWOhh4ZFRMHHJiUnJY+iZWDm4+NKEkJODQomgwWpZqAHc4dQBuZJDiaCxuvug0oaQR0MkQaisQSVUwNXmIReXV1Sw1IA
*/
class A {
public static field = 'A'
public toString() {
return Object.getPrototypeOf(this).constructor.field
}
}
@marcolink
marcolink / date-types.ts
Last active October 1, 2021 14:23
Typescript - Typed date strings using template string literal.
/**
* @desc range 0 - 9
*/
type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
export declare namespace ISO8601 {
/**
* @desc range 00 - 24
*/
type TimeHour = `${'0' | '1' | '2'}${'0' | '1' | '2' | '3' | '4'}`
import * as React from "react";
function useRenderComponent<T>(Component: React.FC, initialProps?: T) {
const [props, setProps] = React.useState<T>(initialProps);
const updateProps = <K extends keyof T>(
field: K,
value: T[K]
) =>
setProps((prevState: T) => ({
...prevState,
@marcolink
marcolink / DESTRUCT-CUSTOM-REACT-HOOKS.md
Last active January 15, 2019 15:22
destructing custom react hooks

Destruct custom react hooks (with typescript)

Bad

const [myValue] = useCustomHook();
//or
const [myValue, setMyValue] = useCustomHook();
//or
const [myValue, setMyValue, isMyValueValid] = useCustomHook();