Estoy usando el tutorial Node Api de Shopify para crear una tienda Redis. Sin embargo, el bloque de código provisto está escrito a máquina y todo mi proyecto está escrito en javascript (React/nextjs). He estado trabajando durante algunas horas para intentar convertir el código para que sea utilizable, pero no puedo hacer que funcione correctamente en mi proyecto. Luchando seriamente con esto.
¿Cómo convertiría el siguiente bloque de código de mecanografiado a javascript?
/* redis-store.ts */ // Import the Session type from the library, along with the Node redis package, and `promisify` from Node import {Session} from '@shopify/shopify-api/dist/auth/session'; import redis from 'redis'; import {promisify} from 'util'; class RedisStore { private client: redis.RedisClient; private getAsync; private setAsync; private delAsync; constructor() { // Create a new redis client this.client = redis.createClient(); // Use Node's `promisify` to have redis return a promise from the client methods this.getAsync = promisify(this.client.get).bind(this.client); this.setAsync = promisify(this.client.set).bind(this.client); this.delAsync = promisify(this.client.del).bind(this.client); } /* The storeCallback takes in the Session, and sets a stringified version of it on the redis store This callback is used for BOTH saving new Sessions and updating existing Sessions. If the session can be stored, return true Otherwise, return false */ storeCallback = async (session: Session) => { try { // Inside our try, we use the `setAsync` method to save our session. // This method returns a boolean (true if successful, false if not) return await this.setAsync(session.id, JSON.stringify(session)); } catch (err) { // throw errors, and handle them gracefully in your application throw new Error(err); } }; /* The loadCallback takes in the id, and uses the getAsync method to access the session data If a stored session exists, it's parsed and returned Otherwise, return undefined */ loadCallback = async (id: string) => { try { // Inside our try, we use `getAsync` to access the method by id // If we receive data back, we parse and return it // If not, we return `undefined` let reply = await this.getAsync(id); if (reply) { return JSON.parse(reply); } else { return undefined; } } catch (err) { throw new Error(err); } }; /* The deleteCallback takes in the id, and uses the redis `del` method to delete it from the store If the session can be deleted, return true Otherwise, return false */ deleteCallback = async (id: string) => { try { // Inside our try, we use the `delAsync` method to delete our session. // This method returns a boolean (true if successful, false if not) return await this.delAsync(id); } catch (err) { throw new Error(err); } }; } // Export the class export default RedisStore;Simplemente guarde todo ese código mecanografiado en un archivo .ts (probablemente redis-store.ts). luego use el compilador de mecanografiado para convertir a su versión de javascript simplemente ejecutando el comando tsc como se muestra a continuación
tsc redis-store.tspara obtener más opciones de compilador, visite a continuación https://www.typescriptlang.org/docs/handbook/compiler-options.html
Básicamente, debe deshacerse de todos los tipos (sesión y cadena) y cambiar private a # , tal vez algo como esto:
/* redis-store.js */ import redis from 'redis'; import {promisify} from 'util'; class RedisStore { #client; #getAsync; #setAsync; #delAsync; constructor() { // Create a new redis client this.client = redis.createClient(); this.getAsync = promisify(this.client.get).bind(this.client); this.setAsync = promisify(this.client.set).bind(this.client); this.delAsync = promisify(this.client.del).bind(this.client); } storeCallback = async (session) => { try { // Inside our try, we use the `setAsync` method to save our session. // This method returns a boolean (true if successful, false if not) return await this.setAsync(session.id, JSON.stringify(session)); } catch (err) { // throw errors, and handle them gracefully in your application throw new Error(err); } }; /* The loadCallback takes in the id, and uses the getAsync method to access the session data If a stored session exists, it's parsed and returned Otherwise, return undefined */ loadCallback = async (id) => { try { // Inside our try, we use `getAsync` to access the method by id // If we receive data back, we parse and return it // If not, we return `undefined` let reply = await this.getAsync(id); if (reply) { return JSON.parse(reply); } else { return undefined; } } catch (err) { throw new Error(err); } }; /* The deleteCallback takes in the id, and uses the redis `del` method to delete it from the store If the session can be deleted, return true Otherwise, return false */ deleteCallback = async (id) => { try { // Inside our try, we use the `delAsync` method to delete our session. // This method returns a boolean (true if successful, false if not) return await this.delAsync(id); } catch (err) { throw new Error(err); } }; } // Export the class export default RedisStore;