README
@xunnamius/mongo-types
This package contains several TypeScript utility types and helper functions for use with MongoDB-based projects.
Install
npm install --save-dev @xunnamius/mongo-types
Usage
You can use this library's exports in your TypeScript projects like so:
import type { DUMMY_BEARER_TOKEN } from '@xunnamius/mongo-types';
// ...
await specialAuthentication({ token: { bearer: DUMMY_BEARER_TOKEN } });
Type and Constant Glossary
This package exports the following:
- NULL_BEARER_TOKEN
- MACHINE_BEARER_TOKEN
- DUMMY_BEARER_TOKEN
- BANNED_BEARER_TOKEN
- DEV_BEARER_TOKEN
- InternalAuthEntry
- InternalAuthBearerEntry
- InternalRequestLogEntry
- InternalLimitedLogEntry
NULL_BEARER_TOKEN
This string represents a token that is guaranteed never to appear in dummy data
generated during tests. In production, this string can be used to represent a
null
or non-existent token. This string cannot be used for authenticated HTTP
access to the API.
import type { NULL_BEARER_TOKEN } from '@xunnamius/mongo-types';
if (doAuth({ bearerToken: NULL_BEARER_TOKEN })) {
// ...
} else {
// ...
}
MACHINE_BEARER_TOKEN
This string represents a token that is used by database initialization and activity simulation scripts. This string cannot be used for authenticated HTTP access to the API.
import type { MACHINE_BEARER_TOKEN } from '@xunnamius/mongo-types';
if (doAuth({ bearerToken: MACHINE_BEARER_TOKEN })) {
// ...
} else {
// ...
}
DUMMY_BEARER_TOKEN
This string represents a token that allows authenticated API access only when
running in a test environment (i.e. NODE_ENV=test
). This string cannot be used
for authenticated HTTP access to the API in production.
import type { DUMMY_BEARER_TOKEN } from '@xunnamius/mongo-types';
if (doAuth({ bearerToken: DUMMY_BEARER_TOKEN })) {
// ...
} else {
// ...
}
BANNED_BEARER_TOKEN
This string represents a token that is guaranteed to be rate limited when
running in a test environment (i.e. NODE_ENV=test
). This string cannot be used
for authenticated HTTP access to the API in production.
import type { BANNED_BEARER_TOKEN } from '@xunnamius/mongo-types';
if (doAuth({ bearerToken: BANNED_BEARER_TOKEN })) {
// ...
} else {
// ...
}
DEV_BEARER_TOKEN
This string represents a token that can be used to authenticate with local and non-production deployments. This string cannot be used for authenticated HTTP access to the API in production.
import type { DEV_BEARER_TOKEN } from '@xunnamius/mongo-types';
if (doAuth({ bearerToken: DEV_BEARER_TOKEN })) {
// ...
} else {
// ...
}
InternalAuthEntry
This type represents a basic credentials entry in the well-known "auth" collection. More complex credential types must extend from this base type.
import type { InternalAuthEntry } from '@xunnamius/mongo-types';
export async function isAuthOk({ token }: { token: Record<string, unknown> }) {
return (await getDb({ name: 'system' }))
.collection<InternalAuthEntry>('auth')
.findOne({ token })
.then((r) => !!r);
}
InternalAuthBearerEntry
This type represents a bearer token entry in the well-known "auth" collection.
import type { InternalAuthEntry } from '@xunnamius/mongo-types';
export async function isAuthOk({ token }: { token: { bearer: string } }) {
return (await getDb({ name: 'system' }))
.collection<InternalAuthBearerEntry>('auth')
.findOne({ token })
.then((r) => !!r);
}
InternalRequestLogEntry
This type represents an entry in the well-known "request log" collection.
import type { InternalRequestLogEntry } from '@xunnamius/mongo-types';
export async function insertLog({ req }: { req: NextApiRequest }) {
await (await getDb({ name: 'system' }))
.collection<InternalRequestLogEntry>('request-log')
.insertOne({
ip: getClientIp(req),
token,
method: req.method || null,
route: req.url || null,
resStatusCode: res.statusCode as HttpStatusCode,
time: Date.now()
});
}
InternalLimitedLogEntry
This type represents an entry in the well-known "limited log" collection.
import type { InternalLimitedLogEntry } from '@xunnamius/mongo-types';
export async function isRateLimited(ip: string | null, token: string | null) {
return !!(await (
await getDb({ name: 'system' })
)
.collection<InternalLimitedLogEntry>('limited-log-mview')
.find({
$or: [...(ip ? [{ ip }] : []), ...(token ? [{ token }] : [])],
until: { $gt: Date.now() } // ? Skip the recently unbanned
})
.sort({ until: -1 })
.limit(1)
.next());
}
Function Glossary
The following functions are available: (none yet)
Documentation
Further documentation can be found under docs/
.
License
Contributing and Support
New issues and pull requests are always welcome and greatly appreciated! 🤩 Just as well, you can star 🌟 this project to let me know you found it useful! ✊🏿 Thank you!
See CONTRIBUTING.md and SUPPORT.md for more information.