Skip to content

Instantly share code, notes, and snippets.

@michielvaneerd
michielvaneerd / mycrypt.dart
Created January 30, 2024 10:02
Encrypt and decrypt in Dart using the cryptography package
import 'dart:convert' as convert;
import 'package:cryptography/cryptography.dart';
class MyCrypt {
final AesGcm aesGcm;
MyCrypt() : aesGcm = AesGcm.with256bits();
static const nonceLength = 12;
static const macLength = 16;
@michielvaneerd
michielvaneerd / useFetchCache.js
Last active November 27, 2020 09:37
ReactJS hook for fetch an API response and store it to the cache
/**
* The useFetchCache hook: fetch API response and save it in the cache.
* The next time it uses the cached response.
**/
const _cache = {};
export default function useFetchCache(key) {
const [data, setData] = useState((key in _cache) ? _cache[key] : null);
const [error, setError] = useState(null);
@michielvaneerd
michielvaneerd / docker-compose.yml
Created December 21, 2019 15:07
Wordpress docker file with node and npm
# Example of Wordpress docker installation for Gutenberg plugin development where you need node and npm.
# Used for plugin development.
# Put this docker-compose file in the root of your plugin directory.
# This setup removes the src and node_modules directory from the mount to the container.
# This will improve the performance for Mac, because it doesn't has to sync so much files.
# After running this setup, you can just use npm install on your host.
version: '3.3'
services:
@michielvaneerd
michielvaneerd / Backbone and JSONP
Last active November 4, 2021 16:27
Fetching a Backbone collection with JSONP is really simple. It turns out you only need to override the sync method (to set the dataType to jsonp). In this case I also had to override the parse method, because the response consists of more than the models. This example uses the Discogs API to search for artists.
var Artist = Backbone.Model.extend();
var Artists = Backbone.Collection.extend({
model : Artist,
url : "http://api.discogs.com/database/search?type=artist",
sync : function(method, collection, options) {
// By setting the dataType to "jsonp", jQuery creates a function
// and adds it as a callback parameter to the request, e.g.:
// [url]&callback=jQuery19104472605645155031_1373700330157&q=bananarama
// If you want another name for the callback, also specify the