JWT Autorization / Logging in and registering
This commit is contained in:
39
node_modules/translate/src/engines.js
generated
vendored
Normal file
39
node_modules/translate/src/engines.js
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
// Translation engines
|
||||
// Handle different translations differently to allow for extra flexibility
|
||||
|
||||
|
||||
const google = {
|
||||
needkey: true,
|
||||
fetch: ({ key, from, to, text }) => [
|
||||
`https://translation.googleapis.com/language/translate/v2?key=${key}&format=text&source=${from}&target=${to}&q=${encodeURIComponent(text)}`,
|
||||
{ method: 'POST' }
|
||||
],
|
||||
parse: res => res.json().then(body => {
|
||||
if (body.error) {
|
||||
throw new Error(body.error.errors[0].message);
|
||||
}
|
||||
body = body.data.translations[0];
|
||||
if (!body) {
|
||||
throw new Error('Translation not found');
|
||||
}
|
||||
return body.translatedText;
|
||||
})
|
||||
};
|
||||
|
||||
|
||||
const yandex = {
|
||||
needkey: true,
|
||||
fetch: ({ key, from, to, text }) => [
|
||||
`https://translate.yandex.net/api/v1.5/tr.json/translate?key=${key}&lang=${from}-${to}&text=${encodeURIComponent(text)}`,
|
||||
{ method: 'POST', body: '' }
|
||||
],
|
||||
parse: res => res.json().then(body => {
|
||||
if (body.code !== 200) {
|
||||
throw new Error(body.message);
|
||||
}
|
||||
return body.text[0];
|
||||
})
|
||||
};
|
||||
|
||||
|
||||
export default { google, yandex };
|
||||
92
node_modules/translate/src/index.js
generated
vendored
Normal file
92
node_modules/translate/src/index.js
generated
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
// translate.js
|
||||
// Translate text into different languages;
|
||||
|
||||
// Load a language parser to allow for more flexibility in the language choice
|
||||
import language from "./language";
|
||||
|
||||
// Load the default engines for translation
|
||||
import engines from "./engines";
|
||||
|
||||
// Cache the different translations to avoid resending requests
|
||||
import cache from "../lib/cache";
|
||||
|
||||
// Main function
|
||||
const Translate = function(options = {}) {
|
||||
if (!(this instanceof Translate)) {
|
||||
return new Translate(options);
|
||||
}
|
||||
|
||||
const defaults = {
|
||||
from: "en",
|
||||
to: "en",
|
||||
cache: undefined,
|
||||
language: language,
|
||||
engines: engines,
|
||||
engine: "google",
|
||||
keys: {}
|
||||
};
|
||||
|
||||
const translate = (text, opts = {}) => {
|
||||
// Load all of the appropriate options (verbose but fast)
|
||||
// Note: not all of those *should* be documented since some are internal only
|
||||
if (typeof opts === "string") opts = { to: opts };
|
||||
opts.text = text;
|
||||
opts.from = language(opts.from || translate.from);
|
||||
opts.to = language(opts.to || translate.to);
|
||||
opts.cache = opts.cache || translate.cache;
|
||||
opts.engines = opts.engines || {};
|
||||
opts.engine = opts.engine || translate.engine;
|
||||
opts.id = opts.id || `${opts.from}:${opts.to}:${opts.engine}:${opts.text}`;
|
||||
opts.keys = opts.keys || translate.keys || {};
|
||||
for (let name in translate.keys) {
|
||||
// The options has stronger preference than the global value
|
||||
opts.keys[name] = opts.keys[name] || translate.keys[name];
|
||||
}
|
||||
opts.key = opts.key || translate.key || opts.keys[opts.engine];
|
||||
|
||||
// TODO: validation for few of those
|
||||
|
||||
// Use the desired engine
|
||||
const engine = opts.engines[opts.engine] || translate.engines[opts.engine];
|
||||
|
||||
// If it is cached return ASAP
|
||||
const cached = cache.get(opts.id);
|
||||
if (cached) return Promise.resolve(cached);
|
||||
|
||||
// Target is the same as origin, just return the string
|
||||
if (opts.to === opts.from) {
|
||||
return Promise.resolve(opts.text);
|
||||
}
|
||||
|
||||
// Will load only for Node.js and use the native function on the browser
|
||||
if (typeof fetch === "undefined") {
|
||||
try {
|
||||
global.fetch = require("node-fetch");
|
||||
} catch (error) {
|
||||
console.warn("Please make sure node-fetch is available");
|
||||
}
|
||||
}
|
||||
|
||||
if (engine.needkey && !opts.key) {
|
||||
throw new Error(
|
||||
`The engine "${opts.engine}" needs a key, please provide it`
|
||||
);
|
||||
}
|
||||
|
||||
const fetchOpts = engine.fetch(opts);
|
||||
return fetch(...fetchOpts)
|
||||
.then(engine.parse)
|
||||
.then(translated => cache.put(opts.id, translated, opts.cache));
|
||||
};
|
||||
|
||||
for (let key in defaults) {
|
||||
translate[key] =
|
||||
typeof options[key] === "undefined" ? defaults[key] : options[key];
|
||||
}
|
||||
return translate;
|
||||
};
|
||||
|
||||
// Small hack needed for Webpack/Babel: https://github.com/webpack/webpack/issues/706
|
||||
const exp = new Translate();
|
||||
exp.Translate = Translate;
|
||||
export default exp;
|
||||
42
node_modules/translate/src/language.js
generated
vendored
Normal file
42
node_modules/translate/src/language.js
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
// Relevant ISO: ISO 639-1 & ISO 639-2. Google uses the ISO 639-1.
|
||||
// Valid ISO 639-1 codes
|
||||
// https://www.loc.gov/standards/iso639-2/php/code_list.php
|
||||
// Extract these with this code (after loading https://superdom.site/ )
|
||||
// [...dom.table[1].querySelectorAll('tbody tr')].slice(1).filter(row => !/^\s*$/.test(row.querySelector('td:nth-child(2)').textContent)).map((row, i) => `"${row.querySelector('td:nth-child(2)').textContent}", ${i % 12 === 11 ? '\n' : ''}`).join('');
|
||||
import iso from '../data/iso';
|
||||
|
||||
// Parsed from here: https://github.com/wooorm/iso-639-2/blob/master/index.json
|
||||
import iso2 from '../data/iso2';
|
||||
|
||||
// Extract these with this code (after loading https://superdom.site/ ) + a lot of manual clean up
|
||||
// [...dom.table[1].querySelectorAll('tbody tr')].slice(1).filter(row => !/^\s*$/.test(row.querySelector('td:nth-child(2)').textContent)).map(row =>
|
||||
// ` "${row.querySelector('td:nth-child(3)').textContent.toLowerCase()}": "${row.querySelector('td:nth-child(2)').textContent.toLowerCase()}",`
|
||||
// ).join('\n');
|
||||
import map from '../data/map';
|
||||
|
||||
// Language parser
|
||||
// @name: a string to be parsed
|
||||
// @output: the normalized language string
|
||||
export default name => {
|
||||
|
||||
// Validate the name string
|
||||
if (typeof name !== 'string') {
|
||||
throw new Error(`The language must be a string, received ${typeof name}`);
|
||||
}
|
||||
// Possible overflow errors
|
||||
if (name.length > 100) {
|
||||
throw new Error(`The language must be a string under 100 characters, received ${name.length}`);
|
||||
}
|
||||
|
||||
// Let's work with lowercase for everything
|
||||
name = name.toLowerCase();
|
||||
|
||||
// Pass it through several possible maps to try to find the right one
|
||||
name = map[name] || iso2[name] || name;
|
||||
|
||||
// Make sure we have the correct name or throw
|
||||
if (!iso.includes(name)) {
|
||||
throw new Error(`The name "${name}" is not part of the ISO 639-1 and we couldn't automatically parse it :(`);
|
||||
}
|
||||
return name;
|
||||
};
|
||||
Reference in New Issue
Block a user