create amnet-shared for web/native shared files

This commit is contained in:
ppc
2024-08-22 09:01:59 +01:00
parent 839d45884a
commit 733fa96b19
6 changed files with 2181 additions and 7 deletions
+2078
View File
File diff suppressed because it is too large Load Diff
+19
View File
@@ -0,0 +1,19 @@
{
"name": "amnet-shared",
"version": "1.0.0",
"private": true,
"description": "AMNet shared library",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "tsup"
},
"dependencies": {
"tsup": "^8.2.4",
"typescript": "^5.5.4"
}
}
+57
View File
@@ -0,0 +1,57 @@
/**
* The latest version of the AMNet API this library supports
*/
export const AMNET_API_VERSION = 1;
export interface AMCardInRequest {
cardId: string;
}
export interface AMServerInfoResponse {
serverAddress: string;
serverName: string;
apiVersion: number;
}
/**
* Performs the card-in request to the specified server
* @param serverAddress The origin address of the server to perform the request against
* @param request The card-in request to send
* @param signal The abort signal to use for the request
*/
export async function performCardIn(serverAddress: string, request: AMCardInRequest, signal: AbortSignal) {
try {
const response = await fetch(`${serverAddress}/card`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(request),
signal: signal
});
return response.ok;
} catch {
return false;
}
}
/**
* Attempts to connect to the specified server and fetch its info
* @param serverAddress The origin address of the server to connect to
* @param abort The abort signal to use for the request (for cancellation)
*/
export async function fetchServerInfo(serverAddress: string, abort: AbortSignal): Promise<AMServerInfoResponse | null> {
try {
const response = await fetch(serverAddress, {
signal: abort
});
return {
...await response.json(),
serverAddress: serverAddress
} satisfies AMServerInfoResponse;
} catch {
return null;
}
}
@@ -1,15 +1,12 @@
export const CARD_ID_REGEX = /^(?:[0-9]{4} ?){5}$/i;
/**
* Generates a random Aime card id.
*/
export function generateAimeCardId(): string {
export function generateCardNumber(): string {
let cardId = '';
while (true) {
// don't start with a 3
const firstDigit = Math.floor(Math.random() * 10);
if (firstDigit !== 3) {
cardId = firstDigit.toString();
break;
@@ -27,6 +24,6 @@ export function generateAimeCardId(): string {
* Validates the provided card id by known parameters
* @param id The card id to validate
*/
export function validateAimeCardId(id: string): boolean {
export function validateCardId(id: string): boolean {
return id[0] !== '3' && CARD_ID_REGEX.test(id);
}
}
+13
View File
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "ES2016",
"module": "CommonJS",
"noEmit": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noUncheckedIndexedAccess": true,
"skipLibCheck": true
},
"include": ["src/**/*.ts"]
}
+10
View File
@@ -0,0 +1,10 @@
import { defineConfig } from "tsup";
export default defineConfig({
entry: ["src/amnet-api.ts", "src/cardgen.ts"],
format: ["cjs", "esm"],
splitting: false,
sourcemap: true,
clean: true,
dts: true,
});