Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

411
Vistas
¿Convertir código mecanografiado en código Javascript?

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;
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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.ts

para obtener más opciones de compilador, visite a continuación https://www.typescriptlang.org/docs/handbook/compiler-options.html

about 4 years ago · Juan Pablo Isaza Denunciar

0

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;
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda