Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
shakibhasan09 committed Aug 23, 2024
0 parents commit d06b748
Show file tree
Hide file tree
Showing 16 changed files with 911 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# Dependencies
node_modules
.pnp
.pnp.js

# Local env files
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

# Testing
coverage

# Turbo
.turbo

# Vercel
.vercel

# Build Outputs
.next/
out/
build
dist


# Debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Misc
.DS_Store
*.pem

# Apps
apps
Empty file added .npmrc
Empty file.
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"eslint.workingDirectories": [
{
"mode": "auto"
}
]
}
81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Turborepo starter

This is an official starter Turborepo.

## Using this example

Run the following command:

```sh
npx create-turbo@latest
```

## What's inside?

This Turborepo includes the following packages/apps:

### Apps and Packages

- `docs`: a [Next.js](https://nextjs.org/) app
- `web`: another [Next.js](https://nextjs.org/) app
- `@repo/ui`: a stub React component library shared by both `web` and `docs` applications
- `@repo/eslint-config`: `eslint` configurations (includes `eslint-config-next` and `eslint-config-prettier`)
- `@repo/typescript-config`: `tsconfig.json`s used throughout the monorepo

Each package/app is 100% [TypeScript](https://www.typescriptlang.org/).

### Utilities

This Turborepo has some additional tools already setup for you:

- [TypeScript](https://www.typescriptlang.org/) for static type checking
- [ESLint](https://eslint.org/) for code linting
- [Prettier](https://prettier.io) for code formatting

### Build

To build all apps and packages, run the following command:

```
cd my-turborepo
pnpm build
```

### Develop

To develop all apps and packages, run the following command:

```
cd my-turborepo
pnpm dev
```

### Remote Caching

Turborepo can use a technique known as [Remote Caching](https://turbo.build/repo/docs/core-concepts/remote-caching) to share cache artifacts across machines, enabling you to share build caches with your team and CI/CD pipelines.

By default, Turborepo will cache locally. To enable Remote Caching you will need an account with Vercel. If you don't have an account you can [create one](https://vercel.com/signup), then enter the following commands:

```
cd my-turborepo
npx turbo login
```

This will authenticate the Turborepo CLI with your [Vercel account](https://vercel.com/docs/concepts/personal-accounts/overview).

Next, you can link your Turborepo to your Remote Cache by running the following command from the root of your Turborepo:

```
npx turbo link
```

## Useful Links

Learn more about the power of Turborepo:

- [Tasks](https://turbo.build/repo/docs/core-concepts/monorepos/running-tasks)
- [Caching](https://turbo.build/repo/docs/core-concepts/caching)
- [Remote Caching](https://turbo.build/repo/docs/core-concepts/remote-caching)
- [Filtering](https://turbo.build/repo/docs/core-concepts/monorepos/filtering)
- [Configuration Options](https://turbo.build/repo/docs/reference/configuration)
- [CLI Usage](https://turbo.build/repo/docs/reference/command-line-reference)
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "unify-payment",
"private": true,
"scripts": {
"build": "turbo build",
"dev": "turbo dev",
"lint": "turbo lint",
"format": "prettier --write \"**/*.{ts,tsx,md}\""
},
"devDependencies": {
"prettier": "^3.2.5",
"turbo": "^2.0.14",
"typescript": "^5.4.5"
},
"packageManager": "[email protected]",
"engines": {
"node": ">=18"
}
}
17 changes: 17 additions & 0 deletions packages/node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@unify-payment/node",
"version": "0.0.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "npx tsc -b",
"dev": "npx tsc -b -w"
},
"devDependencies": {
"@tsconfig/recommended": "^1.0.7",
"typescript": "^5.4.5"
},
"dependencies": {
"stripe": "^16.8.0"
}
}
Empty file added packages/node/readme.md
Empty file.
4 changes: 4 additions & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./lib/common";
export * from "./lib/lemonsqueezy";
export * from "./lib/stripe";
export * from "./types/lemonsqueezy";
26 changes: 26 additions & 0 deletions packages/node/src/lib/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Stripe from "stripe";
import { LemonSqueezy, UnifyLemonSqueezy } from "./lemonsqueezy";
import { UnifyStripe } from "./stripe";

export type UnifyPaymentOptions = {
stripe?: Stripe;
lemonsqueezy?: LemonSqueezy;
};

export class UnifyPayment<T extends UnifyPaymentOptions = UnifyPaymentOptions> {
stripe: T["stripe"] extends Stripe ? UnifyStripe : undefined;

lemonsqueezy: T["lemonsqueezy"] extends LemonSqueezy
? UnifyLemonSqueezy
: undefined;

constructor(options: T) {
this.stripe = new UnifyStripe(options.stripe!) as T["stripe"] extends Stripe
? UnifyStripe
: undefined;

this.lemonsqueezy = new UnifyLemonSqueezy(
options.lemonsqueezy!
) as T["lemonsqueezy"] extends LemonSqueezy ? UnifyLemonSqueezy : undefined;
}
}
49 changes: 49 additions & 0 deletions packages/node/src/lib/lemonsqueezy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { CheckoutCreateParams } from "../types/lemonsqueezy";

export class LemonSqueezy {
constructor(private apiKey: string) {}

getApiBaseUrl() {
return "https://api.lemonsqueezy.com/v1";
}

getApiRequestHeaders() {
return {
Accept: "application/vnd.api+json",
"Content-Type": "application/vnd.api+json",
Authorization: `Bearer ${this.apiKey}`,
};
}
}

export class UnifyLemonSqueezy {
constructor(private lemonSqueezy: LemonSqueezy) {}

private async fetch(
path: string,
params?: { method?: string; headers?: HeadersInit; body?: BodyInit }
) {
return await fetch(`${this.lemonSqueezy.getApiBaseUrl()}${path}`, {
method: params?.method || "GET",
headers: this.lemonSqueezy.getApiRequestHeaders(),
body: params?.body,
});
}

async getCheckoutUrl(body: CheckoutCreateParams) {
const req = await this.fetch("/checkouts", {
method: "POST",
body: JSON.stringify(body),
});

const res = (await req.json()) as
| { data: { attributes: { url: string } } }
| { errors: [{ detail: string }] };

if ("errors" in res) {
throw new Error(res.errors[0].detail);
}

return res.data.attributes.url;
}
}
14 changes: 14 additions & 0 deletions packages/node/src/lib/stripe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Stripe from "stripe";

export class UnifyStripe {
constructor(private stripe: Stripe) {}

async getCheckoutUrl(params: Stripe.Checkout.SessionCreateParams) {
const session = await this.stripe.checkout.sessions.create(params);
if (!session.url) {
throw new Error("Failed to get checkout url");
}

return session.url;
}
}
54 changes: 54 additions & 0 deletions packages/node/src/types/lemonsqueezy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export interface CheckoutCreateParams {
type: "checkouts";
attributes: Attributes;
relationships: Relationships;
}

interface Attributes {
custom_price: number;
product_options: ProductOptions;
checkout_options: CheckoutOptions;
checkout_data: CheckoutData;
expires_at: string;
preview: boolean;
}

interface ProductOptions {
enabled_variants: number[];
}

interface CheckoutOptions {
button_color: string;
}

interface CheckoutData {
discount_code: string;
custom: Custom;
}

interface Custom {
user_id: number;
}

interface Relationships {
store: Store;
variant: Variant;
}

interface Store {
data: Data;
}

interface Data {
type: string;
id: string;
}

interface Variant {
data: Data2;
}

interface Data2 {
type: string;
id: string;
}
9 changes: 9 additions & 0 deletions packages/node/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@tsconfig/recommended",
"compilerOptions": {
"declaration": true,
"outDir": "dist"
},
"include": ["src"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit d06b748

Please sign in to comment.