@sqrzro/addons 4.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,5 @@
1
+ Copyright 2024 Richard Carter
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
4
+
5
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
@@ -0,0 +1,3 @@
1
+ import { AddonCLIFileObject } from './utility/interfaces';
2
+ declare function getConfig(): Promise<AddonCLIFileObject>;
3
+ export default getConfig;
@@ -0,0 +1,21 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ const NAME = 'sqrzro';
4
+ function getConfigPath() {
5
+ const files = fs.readdirSync(process.cwd());
6
+ for (const file of files) {
7
+ if (file.match(new RegExp(`^${NAME}\\.config\\.(js|ts|cjs|mjs)$`))) {
8
+ return path.resolve(process.cwd(), file);
9
+ }
10
+ }
11
+ return null;
12
+ }
13
+ async function getConfig() {
14
+ const configPath = getConfigPath();
15
+ if (!configPath) {
16
+ return {};
17
+ }
18
+ const config = await import(configPath);
19
+ return 'default' in config ? config.default : {};
20
+ }
21
+ export default getConfig;
@@ -0,0 +1,2 @@
1
+ export * from './utility/interfaces';
2
+ export { default as getConfig } from './get-config';
@@ -0,0 +1,2 @@
1
+ export * from './utility/interfaces';
2
+ export { default as getConfig } from './get-config';
@@ -0,0 +1,3 @@
1
+ export interface AddonCLIFileObject {
2
+ schemas?: string[];
3
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ declare class MissingConfigError extends Error {
2
+ constructor(name: string);
3
+ }
4
+ export default MissingConfigError;
@@ -0,0 +1,7 @@
1
+ class MissingConfigError extends Error {
2
+ constructor(name) {
3
+ super(`The config for addon \`${name}\` appears to be missing. Please make sure you have set it up using \`registerAddons\` in your root layout or API route.`);
4
+ this.name = 'MissingConfigError';
5
+ }
6
+ }
7
+ export default MissingConfigError;
@@ -0,0 +1,4 @@
1
+ import { NextRequest, NextResponse } from 'next/server';
2
+ import type { AddonProxyResponse } from './interfaces';
3
+ declare function extendProxy(request: NextRequest, ...fns: Array<(request: NextRequest, response: AddonProxyResponse) => Promise<AddonProxyResponse>>): Promise<NextResponse<unknown>>;
4
+ export default extendProxy;
@@ -0,0 +1,15 @@
1
+ import { NextResponse } from 'next/server';
2
+ async function extendProxy(request, ...fns) {
3
+ const { pathname } = request.nextUrl;
4
+ let response = { data: {} };
5
+ for (const fn of fns) {
6
+ response = await fn(request, response);
7
+ if (response.redirect) {
8
+ return response.redirect === pathname
9
+ ? NextResponse.next()
10
+ : NextResponse.redirect(new URL(response.redirect, request.url));
11
+ }
12
+ }
13
+ return NextResponse.next();
14
+ }
15
+ export default extendProxy;
@@ -0,0 +1,7 @@
1
+ declare function extendRepository<T, A>(repository: T, fn1: (repository: T) => A): A;
2
+ declare function extendRepository<T, A, B>(repository: T, fn1: (repository: T) => A, fn2: (repository: A) => B): B;
3
+ declare function extendRepository<T, A, B, C>(repository: T, fn1: (repository: T) => A, fn2: (repository: A) => B, fn3: (repository: B) => C): C;
4
+ declare function extendRepository<T, A, B, C, D>(repository: T, fn1: (repository: T) => A, fn2: (repository: A) => B, fn3: (repository: B) => C, fn4: (repository: C) => D): D;
5
+ declare function extendRepository<T, A, B, C, D, E>(repository: T, fn1: (repository: T) => A, fn2: (repository: A) => B, fn3: (repository: B) => C, fn4: (repository: C) => D, fn5: (repository: D) => E): E;
6
+ declare function extendRepository<T, A, B, C, D, E, F>(repository: T, fn1: (repository: T) => A, fn2: (repository: A) => B, fn3: (repository: B) => C, fn4: (repository: C) => D, fn5: (repository: D) => E, fn6: (repository: E) => F): F;
7
+ export default extendRepository;
@@ -0,0 +1,4 @@
1
+ function extendRepository(repository, ...fns) {
2
+ return fns.reduce((acc, fn) => fn(acc), repository);
3
+ }
4
+ export default extendRepository;
@@ -0,0 +1,5 @@
1
+ export * from './interfaces';
2
+ export { default as MissingConfigError } from './config/MissingConfigError';
3
+ export { default as extendRepository } from './extend-repository';
4
+ export { default as extendProxy } from './extend-proxy';
5
+ export { default as registerAddons } from './register-addons';
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from './interfaces';
2
+ export { default as MissingConfigError } from './config/MissingConfigError';
3
+ export { default as extendRepository } from './extend-repository';
4
+ export { default as extendProxy } from './extend-proxy';
5
+ export { default as registerAddons } from './register-addons';
@@ -0,0 +1,10 @@
1
+ import type { NodePgDatabase } from 'drizzle-orm/node-postgres';
2
+ export interface AddonProxyResponse {
3
+ data: object;
4
+ redirect?: string | null;
5
+ }
6
+ export type AddonRegisterReturn = {
7
+ register: (db: NodePgDatabase) => void;
8
+ schema?: string;
9
+ };
10
+ export type AddonRegisterFunction<T = object> = (config?: T) => AddonRegisterReturn;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import type { NodePgDatabase } from 'drizzle-orm/node-postgres';
2
+ import { AddonRegisterReturn } from './interfaces';
3
+ declare function registerAddons(db: NodePgDatabase, addons: AddonRegisterReturn[]): void;
4
+ export default registerAddons;
@@ -0,0 +1,6 @@
1
+ function registerAddons(db, addons) {
2
+ for (const addon of addons) {
3
+ addon.register(db);
4
+ }
5
+ }
6
+ export default registerAddons;
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@sqrzro/addons",
3
+ "type": "module",
4
+ "version": "4.0.0-alpha.2",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "license": "ISC",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "./cli": {
14
+ "types": "./dist/cli/index.d.ts",
15
+ "default": "./dist/cli/index.js"
16
+ },
17
+ "./proxy": {
18
+ "types": "./dist/proxy/index.d.ts",
19
+ "default": "./dist/proxy/index.js"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist/**/*"
24
+ ],
25
+ "dependencies": {
26
+ "drizzle-orm": "^0.45.1",
27
+ "next": "^16.1.6",
28
+ "@sqrzro/server": "^4.0.0-alpha.31"
29
+ },
30
+ "devDependencies": {
31
+ "concurrently": "^9.2.1",
32
+ "prettier": "^3.8.1",
33
+ "rimraf": "^6.1.3",
34
+ "tsc-alias": "^1.8.16",
35
+ "typescript": "^5.9.3",
36
+ "@sqrzro/prettier-config": "^4.0.0-alpha.3"
37
+ },
38
+ "scripts": {
39
+ "build": "pnpm clean && tsc && tsc-alias",
40
+ "clean": "rimraf ./dist",
41
+ "dev": "(concurrently \"tsc --watch\" \"tsc-alias -w\")"
42
+ }
43
+ }