Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ dist
node_modules
**/node_modules

# Docker
docker-compose-data

# Environment Variables
.env
.env.*
Expand All @@ -19,6 +22,7 @@ node_modules
# Generated Code
**/generated
**/prisma/schema.prisma
TypeAPI.d.ts

# System Files
.DS_Store
Expand Down
Binary file modified bun.lockb
Binary file not shown.
19 changes: 19 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '3.7'
name: antribute-open-source
services:
postgres:
image: postgres:latest
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
ports:
- '5432:5432'
volumes:
- ./docker-compose-data/postgres:/var/lib/postgresql/data
- ./seed.sql:/docker-entrypoint-initdb.d/init.sql
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U postgres']
interval: 5s
timeout: 5s
retries: 5
21 changes: 21 additions & 0 deletions packages/config-loader/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Antribute, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
19 changes: 19 additions & 0 deletions packages/config-loader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Config Loader

Loads TypeScript, JavaScript, and JSON configs in a Bun and Node friendly manner

## Installation

```bash
bun add @antribute/config-loader
```

## Usage

1. Create a new file called `.antributerc.ts`
1. Add the following
```typescript
import { defineConfig } from '@antribute/backend-core';
export default defineConfig({ auth: { platform: '@antribute/backend-clerk' } });
```
1. Run the Antribute CLI to auto-generate required files
2 changes: 2 additions & 0 deletions packages/config-loader/build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import unbuildConfig from '@antribute/config/unbuild/unbuild.config.base';
export default unbuildConfig;
47 changes: 47 additions & 0 deletions packages/config-loader/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@antribute/config-loader",
"description": "Loads TypeScript, JavaScript, and JSON configs in a Bun and Node friendly manner",
"version": "0.1.0",
"type": "module",
"publishConfig": {
"access": "public"
},
"exports": {
"bun": "./src/index.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"files": [
"dist/**",
"src/**"
],
"types": "./dist/index.d.ts",
"scripts": {
"build": "bunx --bun unbuild",
"clean": "rimraf .turbo && rimraf coverage && rimraf dist && rimraf node_modules && rimraf test-results",
"lint": "eslint --cache ./src",
"test": "bun test"
},
"keywords": [
"antribute",
"antribute-backend",
"antribute-backend-auth",
"clerk"
],
"author": "Antribute, Inc. (https://www.antribute.com)",
"license": "MIT",
"dependencies": {
"@sinclair/typebox": "^0.31.17",
"bundle-require": "^4.0.1",
"esbuild": "^0.17.18",
"joycon": "^3.1.1",
"lodash-es": "^4.17.21",
"type-fest": "^3.5.1"
},
"devDependencies": {
"@antribute/config": "workspace:*",
"typescript": "^5.0.4",
"unbuild": "^2.0.0"
}
}
21 changes: 21 additions & 0 deletions packages/config-loader/src/bun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getConfigFilePath } from './common';
import type { LoadConfigParams } from './types';

export const loadConfigBun = async <ConfigShape extends Record<string, unknown>>(
fileNames: string[],
opts?: LoadConfigParams
): Promise<ConfigShape | null> => {
const path = await getConfigFilePath(fileNames, opts);
if (!path) {
return null;
}

const rawConfig = await import(path);
const configContent = rawConfig.default ?? rawConfig;

if (typeof configContent !== 'object') {
return null;
}

return configContent as ConfigShape;
};
24 changes: 24 additions & 0 deletions packages/config-loader/src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { parse } from 'path';

import Joycon from 'joycon';
import { merge } from 'lodash-es';

import type { LoadConfigParams } from './types';

export const getConfigFilePath = async (fileNames: string[], opts?: LoadConfigParams) => {
const cwd = opts?.cwd ?? process.cwd();
const overridePath = opts?.overrideConfigPath;
const joycon = new Joycon();
const configFile = await joycon.resolve({
files: overridePath ? [overridePath] : fileNames,
cwd,
stopDir: parse(cwd).root,
});

return configFile;
};

export const mergeConfig = <ConfigShape extends Record<string, unknown>>(
configA: Partial<ConfigShape>,
configB: Partial<ConfigShape>
): Partial<ConfigShape> => merge(configA, configB);
70 changes: 70 additions & 0 deletions packages/config-loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { TObject } from '@sinclair/typebox';
import { Value } from '@sinclair/typebox/value';
import type { PartialDeep } from 'type-fest';

import { mergeConfig } from './common';
import { loadConfigBun } from './bun';
import { loadConfigNode } from './node';
import type { LoadConfigParams } from './types';

export const defineConfig = <ConfigShape extends Record<string, unknown>>(
config: PartialDeep<ConfigShape>
) => config;

export const loadConfig = async <ConfigShape extends Record<string, unknown>>(
fileNames: string[],
defaultConfig: ConfigShape,
opts?: LoadConfigParams
): Promise<ConfigShape> => {
let loadedConfig: ConfigShape | null;
if (typeof Bun === 'undefined') {
loadedConfig = await loadConfigNode(fileNames, opts);
} else {
loadedConfig = await loadConfigBun(fileNames, opts);
}
if (!loadedConfig) {
return defaultConfig;
}
return mergeConfig(defaultConfig, loadedConfig) as ConfigShape;
};

export const validateConfig = <ConfigShape extends Record<string, unknown>>(
configSchema: TObject,
config: unknown,
{ castConfig } = { castConfig: true }
) => {
let finalConfig = config;

if (castConfig) {
finalConfig = Value.Cast(configSchema, config);
}
const isConfigValid = Value.Check(configSchema, finalConfig);

if (!isConfigValid) {
throw new Error('Invalid Configuration');
}

return finalConfig as ConfigShape;
};

export interface LoadAndValidateConfigOptions extends LoadConfigParams {
fileNames: string[];
validationSchema: TObject;
}

export const loadAndValidateConfig = async <ConfigShape extends Record<string, unknown>>({
cwd,
fileNames,
overrideConfigPath,
validationSchema,
}: LoadAndValidateConfigOptions): Promise<ConfigShape> => {
const config = await loadConfig<ConfigShape>(
fileNames,
Value.Create(validationSchema) as ConfigShape,
{
cwd,
overrideConfigPath,
}
);
return validateConfig<ConfigShape>(validationSchema, config);
};
22 changes: 22 additions & 0 deletions packages/config-loader/src/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { bundleRequire } from 'bundle-require';
import { getConfigFilePath } from './common';
import type { LoadConfigParams } from './types';

export const loadConfigNode = async <ConfigShape extends Record<string, unknown>>(
fileNames: string[],
opts?: LoadConfigParams
): Promise<ConfigShape | null> => {
const path = await getConfigFilePath(fileNames, opts);
if (!path) {
return null;
}

const rawConfig = await bundleRequire({ filepath: path });
const configContent = rawConfig.mod.default ?? rawConfig.mod;

if (typeof configContent !== 'object') {
return null;
}

return configContent as ConfigShape;
};
4 changes: 4 additions & 0 deletions packages/config-loader/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface LoadConfigParams {
cwd?: string;
overrideConfigPath?: string;
}
9 changes: 9 additions & 0 deletions packages/config-loader/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@antribute/config/tsconfig/tsconfig.base.json",
"include": ["src/**/*.ts"],
"exclude": ["node_modules"],
"compilerOptions": {
"baseUrl": "./src",
"outDir": "./dist"
}
}
9 changes: 9 additions & 0 deletions packages/typeapi/.typeapirc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from './src';

const config = defineConfig({
database: {
connectionString: 'postgresql://postgres:password@localhost:5432/typeapi',
},
server: { rootDir: 'example' },
});
export default config;
21 changes: 21 additions & 0 deletions packages/typeapi/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Antribute, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
26 changes: 26 additions & 0 deletions packages/typeapi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# TypeAPI

Like your Favorite Backend Framework but Fully Typed

## Installation

```bash
pnpm i @antribute/typeapi
```

## Getting Started

Run `pnpm exec typeapi create`

When running this command, you'll see the following prompts

```
What is your project named? my-app
Would you like to use TypeScript? No / Yes
Would you like to use ESLint? No / Yes
Would you like to use Tailwind CSS? No / Yes
Would you like to use `src/` directory? No / Yes
Would you like to use App Router? (recommended) No / Yes
Would you like to customize the default import alias (@/*)? No / Yes
What import alias would you like configured? @/*
```
2 changes: 2 additions & 0 deletions packages/typeapi/build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import unbuildConfig from '@antribute/config/unbuild/unbuild.config.react';
export default unbuildConfig;
5 changes: 5 additions & 0 deletions packages/typeapi/example/migrations/0000_flawless_mach_iv.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE IF NOT EXISTS "tasks" (
"id" serial PRIMARY KEY NOT NULL,
"isCompleted" boolean DEFAULT false,
"name" text NOT NULL
);
44 changes: 44 additions & 0 deletions packages/typeapi/example/migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"version": "5",
"dialect": "pg",
"id": "f3a052f9-bed1-44e9-9fc9-25f569de7fbc",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"tasks": {
"name": "tasks",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"isCompleted": {
"name": "isCompleted",
"type": "boolean",
"primaryKey": false,
"notNull": false,
"default": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
Loading