chore: add id validator

This commit is contained in:
dsrkafuu 2022-03-13 22:28:59 +08:00
parent f2fde62fc4
commit e0c3e457a5
4 changed files with 10 additions and 2 deletions

View File

@ -39,7 +39,7 @@ https://count.dsrkafuu.net/{id}?theme={asoul,gelbooru,moebooru,rule34}&length={1
3. `{&length}`: Number between `1-10` (default: `7`) or string `auto`
4. `{&add}`: Controls whether make the counter count or not (default: `1`)
Recommend to use `user:usage` like string as ID for better management and potential issue tracking.
Make a pull request to add your id in `ids.json` to use the free public counter. Recommend to use `user:usage` like string as ID for better management and potential issue tracking.
**API Endpoints**

4
ids.json Normal file
View File

@ -0,0 +1,4 @@
{
"dsrkafuu:demo": "Demo for GitHub & preview site.",
"dsrkafuu:home": "Personal blog & GitHub page."
}

View File

@ -1,7 +1,7 @@
{
"private": true,
"name": "moe-counter-cf",
"version": "2.1.0",
"version": "2.2.0",
"description": "Fork of Moe Counter for fast global access powered by Cloudflare Workers.",
"author": "DSRKafuU <dsrkafuu@outlook.com> (https://dsrkafuu.net)",
"license": "MIT",

View File

@ -1,5 +1,6 @@
import { encode } from 'base64-arraybuffer';
import { ResError } from './response';
import ids from '../ids.json';
/**
* @param {string} str
@ -18,6 +19,9 @@ export function validateID(id) {
if (!/^[a-z0-9:.@_-]{1,256}$/i.test(id)) {
throw new ResError(400, 'Invalid Counter ID');
}
if (!ids[id]) {
throw new ResError(400, 'Unregistered Counter ID');
}
return id;
}