nanoid#customAlphabet TypeScript Examples

The following examples show how to use nanoid#customAlphabet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: unique-id-from-path.ts    From one-platform with MIT License 5 votes vote down vote up
nanoid = customAlphabet('0123456789abcdef', 6)
Example #2
Source File: demoAskScriptServer.ts    From askql with MIT License 5 votes vote down vote up
nanoid = customAlphabet('1234567890abcdef', 8)
Example #3
Source File: askscript.ts    From askql with MIT License 5 votes vote down vote up
exports.handler = async function (event: any, context: any, callback: any) {
  const nanoid = customAlphabet('1234567890abcdef', 8);

  const baseEnvironment = {
    resources: { ...builtInResources, ...customResources },
    values: customValues,
  };

  if (event.httpMethod !== 'POST') {
    sendJson(callback, 404);
    return;
  }

  const body = JSON.parse(event.body);

  if (typeof body !== 'object') {
    sendJson(callback, 400, {
      message: 'Please send JSON data',
    });
    return;
  }

  if (!('code' in body) || typeof body.code !== 'string') {
    sendJson(callback, 400, {
      message: 'Please send Askscript code in "code" property',
    });
    return;
  }
  const code: AskScriptCode = body.code;

  let askCode;
  let askCodeSource;

  const id = nanoid();

  try {
    console.log(id + ' -- ' + new Date().toString());
    console.log(id + ' -- ' + chalk.grey(`➡️ ${code}`));

    askCode = parseAskScript(code);

    askCodeSource = askCodeToSource(askCode);
  } catch (e) {
    console.error(id + ' -- ' + new Date().toString());
    console.error(id + ' -- ' + code);
    console.error(id + ' -- ' + e);
    console.error('\n\n');

    sendJson(callback, 400, {
      message: 'Could not compile your AskScript code',
      error: e.toString(),
    });
    return;
  }

  try {
    const result = await runUntyped(baseEnvironment, askCode, []);

    console.log(id + ' -- ' + chalk.grey(`⬅️ ${JSON.stringify(result)}`));
    console.log('\n\n');
    sendJson(callback, 200, {
      askCodeSource,
      message: 'Code run successfully',
      result,
    });
  } catch (e) {
    console.error(id + ' -- ' + new Date().toString());
    console.error(id + ' -- ' + code);
    console.error(id + ' -- ' + e);
    console.error('\n\n');

    sendJson(callback, 400, {
      message: 'Could not run your code',
      error: e.toString(),
    });
  }
};
Example #4
Source File: middleware.ts    From aurora-relayer with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
nanoid = customAlphabet('6789BCDFGHJKLMNPQRTWbcdfghjkmnpqrtwz', 16)
Example #5
Source File: randomId.ts    From engine with MIT License 5 votes vote down vote up
nanoid = customAlphabet(
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_",
  15
)
Example #6
Source File: create.ts    From server with GNU General Public License v3.0 5 votes vote down vote up
alphaUid = customAlphabet(ALPHAS, LENGTH)
Example #7
Source File: create.ts    From server with GNU General Public License v3.0 5 votes vote down vote up
alphaNumUid = customAlphabet(ALPHANUMS, LENGTH)
Example #8
Source File: uid.ts    From server with GNU General Public License v3.0 5 votes vote down vote up
longid = customAlphabet(CUSTOM_ALPHA, LONG_LENGTH)
Example #9
Source File: uid.ts    From server with GNU General Public License v3.0 5 votes vote down vote up
shortid = customAlphabet(ALPHANUMS, SHORT_LENGTH)
Example #10
Source File: uid.ts    From server with GNU General Public License v3.0 5 votes vote down vote up
otpGenerate = customAlphabet(OTPALPHS, SHORT_LENGTH)
Example #11
Source File: index.tsx    From rabet-extension with GNU General Public License v3.0 4 votes vote down vote up
Backup = ({ onClose, needTitle }: BackupProps) => {
  const dlRef = useRef(null);
  const [user, accounts] = useTypedSelector((store) => [
    store.user,
    store.accounts,
  ]);
  const [fileDownloadUrl, setFileDownloadUrl] = useState('');
  const [id, setId] = useState('');

  useEffect(() => {
    const nanoid = customAlphabet(urlAlphabet, 10);

    setId(nanoid(20));
  }, []);

  const onSubmit = (values: FormValues) => {
    if (user.password !== values.password) {
      return {
        password: 'Password is incorrect',
      };
    }

    const accountsJSON = JSON.stringify(accounts, null, 2);
    const jsonEncrypted = encrypt(id, accountsJSON);
    const blob = new Blob([jsonEncrypted]);
    const fileDlUrl = URL.createObjectURL(blob);

    setFileDownloadUrl(fileDlUrl);

    setTimeout(() => {
      dlRef?.current?.click();

      onClose();
    }, 100);

    return {};
  };

  return (
    <S.Container style={{ maxWidth: '460px' }}>
      {needTitle && (
        <PageTitle
          isSetting
          padding="0"
          title="Backup"
          onClose={onClose}
        />
      )}

      <S.info>
        Save the key and download the backup file. With this file and
        key, you will import all wallets when you want.
      </S.info>
      <div style={{ marginTop: '24px' }}>
        <TooltipLabel
          text="Key"
          tooltipText="Please make sure you save this key because the backup file will be encrypted with it, and if you lose it, you will not be able to import your wallets."
        />
        <S.Box>
          {id}
          <S.Copy>
            <CopyText fullIcon text={id} />
          </S.Copy>
        </S.Box>
      </div>
      <Form
        onSubmit={onSubmit}
        render={({
          submitError,
          handleSubmit,
          submitting,
          pristine,
        }) => (
          <form
            className="form"
            onSubmit={handleSubmit}
            autoComplete="off"
          >
            <Field name="password">
              {({ input, meta }) => (
                <div>
                  <S.Label>Password</S.Label>
                  <Input
                    type="password"
                    placeholder="Enter your password"
                    size="medium"
                    variant="password"
                    input={input}
                    meta={meta}
                    autoFocus
                  />
                </div>
              )}
            </Field>

            {submitError && (
              <div className="error">{submitError}</div>
            )}

            <ButtonContainer btnSize={120} mt={32} justify="end">
              <Button
                type="submit"
                variant="primary"
                size="medium"
                content="Download"
                disabled={pristine || submitting}
              />
            </ButtonContainer>

            <a
              className="hidden"
              download="backup.txt"
              href={fileDownloadUrl}
              ref={dlRef}
            >
              download it
            </a>
          </form>
        )}
      />
    </S.Container>
  );
}