Skip to content

Instantly share code, notes, and snippets.

View u007's full-sized avatar
🏠
Working from home

James u007

🏠
Working from home
View GitHub Profile
@u007
u007 / middleware - client.ts
Last active August 26, 2024 03:50
nuxt3 prerender on server side without reloading on frontend
export default defineNuxtRouteMiddleware((to, from) => {
if (import.meta.server) return
const nav = useNavStore();
// console.log('middlwae client', nav.clientSide)
if (nav.clientSide) {
return;
}
setTimeout(() => {
nav.setClientSide(true);
@u007
u007 / README.md
Created February 1, 2024 17:44
fix cpanel permission

to fix all permissions

chmod +x fixperms
for i in `ls -A /var/cpanel/users` ; do ./fixperms $i ; done
@u007
u007 / trpc.ts
Created January 30, 2024 08:27
trpc on cloud function
import { inferAsyncReturnType, initTRPC, TRPCError } from '@trpc/server';
import * as trpcExpress from '@trpc/server/adapters/express';
import express from 'express';
import dotenv from 'dotenv';
import { appRouter } from './router/server';
import cors, { CorsOptions } from 'cors';
import jsonwebtoken from 'jsonwebtoken';
// import * as functions from 'firebase-functions';
import type { HttpFunction } from '@google-cloud/functions-framework';
@u007
u007 / refLocal.ts
Created November 22, 2023 06:05
localStorage composable ref
export const useLocal = <T>(field: string, defaultData: T, prefix = 'state.') => {
const value = ref(defaultData)
const name = `${prefix}${field}`
watch(value, (v) => {
// console.log('useLocal updated', name, v)
localStorage.setItem(name, JSON.stringify(v))
}, { deep: true })
if (typeof window === 'undefined') {
return value
@u007
u007 / Xx.tsx
Last active November 20, 2023 15:31
vue3 with tsx
import { defineComponent, defineProps, defineEmits, defineExpose, ref } from 'vue'
export default defineComponent({
props: {
},
emits: {
update: (value: number) => true,
},
setup (props, { emit }) {
@u007
u007 / count.ts
Created July 24, 2023 02:41
openai get token count via TS
import * as tiktoken from 'tiktoken'
import { TiktokenModel } from 'tiktoken'
// @thanks https://stackoverflow.com/questions/76216113/how-can-i-count-tokens-before-making-api-call
export const calculateTokenCount = async (text: string, model : TiktokenModel = 'gpt-3.5-turbo') => {
try {
const tk = await tiktoken.encoding_for_model(model)
const encoded = tk.encode(text)
return encoded.length
} catch (error: any) {
@u007
u007 / setup.php
Created July 13, 2023 03:17
php code to generate .env from elasticbeanstalk environment variables
<?php
// setup .env from $_ENV
file_put_contents('/var/www/html/.env', '');
foreach ($_ENV as $key => $value)
{
if (
strpos($key, 'PHP_') === 0 ||
strpos($key, 'NOTIFY_') === 0 ||
strpos($key, 'HTTP_') === 0 ||
@u007
u007 / Makefile
Created October 1, 2022 11:22
upgrade package.json to all compatible version
upgrade:
npx ts-node-esm ./upgrade.ts
npm update -S
@u007
u007 / multerconfig.js
Last active October 12, 2021 07:05
multer s3 contentType fix
import stream from 'stream';
const mime = require('mime-types');
import aws from 'aws-sdk';
import multer from 'multer';
import multerS3 from 'multer-s3';
multer({
//...,
storage: multerS3({
@u007
u007 / api_category_config_schema.graphql
Last active February 6, 2021 13:53
strapi graphql with nested field
const { find2 } = require('../controllers/category')
module.exports = {
definition: `
type Category2 {
id: ID!
created_at: DateTime!
updated_at: DateTime!
Title: String
Icon: String