-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bb53ee
commit b55ca42
Showing
3 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace NodeJS { | ||
interface ProcessEnv { | ||
FIGMA_KIT_URL: string | ||
LEMON_SQUEEZY_API_KEY: string | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { HttpClient, HttpClientRequest, HttpClientResponse } from '@effect/platform' | ||
import { Schema } from '@effect/schema' | ||
import { Effect, Schedule, pipe } from 'effect' | ||
import type { NextRequest } from 'next/server' | ||
|
||
const Order = Schema.Struct({ | ||
data: Schema.optional( | ||
Schema.Struct({ | ||
attributes: Schema.Struct({ | ||
refunded: Schema.Boolean, | ||
}), | ||
}), | ||
), | ||
}) | ||
|
||
const { FIGMA_KIT_URL, LEMON_SQUEEZY_API_KEY } = process.env | ||
|
||
export const GET = async (req: NextRequest) => { | ||
const { searchParams } = new URL(req.url) | ||
|
||
const isValid = await Effect.runPromise( | ||
pipe( | ||
Effect.fromNullable(searchParams.get('order_id')), | ||
Effect.flatMap((orderId) => | ||
pipe( | ||
HttpClientRequest.get(`https://api.lemonsqueezy.com/v1/orders/${orderId}`, { | ||
headers: { | ||
Accept: 'application/vnd.api+json', | ||
'Content-Type': 'application/vnd.api+json', | ||
Authorization: `Bearer ${LEMON_SQUEEZY_API_KEY}`, | ||
}, | ||
}), | ||
HttpClient.fetchOk, | ||
HttpClientResponse.schemaBodyJsonScoped(Order), | ||
Effect.timeout('1 seconds'), | ||
Effect.retry(Schedule.exponential(200).pipe(Schedule.compose(Schedule.recurs(3)))), | ||
), | ||
), | ||
Effect.map((data) => !data.data?.attributes.refunded), | ||
Effect.catchAll(() => Effect.succeed(false)), | ||
), | ||
) | ||
|
||
return Response.redirect(isValid ? FIGMA_KIT_URL : 'https://park-ui.com/404') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters