Skip to content

Commit 8e15f75

Browse files
committed
wip
1 parent ea2cf82 commit 8e15f75

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+214
-845
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "module",
66
"scripts": {
77
"build": "turbo build",
8-
"build:pkg": "turbo build --filter=./packages/* --filter=!./packages/cobalt --filter=!./packages/cobalt-next && turbo build --filter=./packages/cobalt --filter=./packages/cobalt-next",
8+
"build:pkg": "turbo build --filter=./packages/* --filter=!./packages/cobalt --filter=!./packages/gate_next --filter=!./packages/gate_tan && turbo build --filter=./packages/cobalt --filter=./packages/gate_next --filter=./packages/gate_tan",
99
"dev": "turbo dev",
1010
"dev:playground": "turbo dev --filter=./playground/*",
1111
"lint": "turbo lint",

packages/blueprint_lib/src/types.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,31 @@ import type { MultipartFile } from '@adonisjs/bodyparser/types'
44
import { Constructor } from '@adonisjs/core/types/container'
55

66
export type EndpointIO = {
7-
output: Record<string, any>
8-
input?: {
9-
params?: Record<string, any>
10-
query?: Record<string, any>
11-
headers?: Record<string, any>
12-
cookies?: Record<string, any>
13-
[key: string]: any // Allow for other input properties
14-
}
7+
output: any
8+
input: any
159
}
1610

1711
export type Endpoint<IO extends EndpointIO> = {
1812
url: (options?: {
19-
params?: NonNullable<IO['input']>['params']
20-
query?: NonNullable<IO['input']>['query']
13+
params?: 'input' extends keyof IO
14+
? 'params' extends keyof IO['input']
15+
? IO['input']['params']
16+
: undefined
17+
: undefined
18+
query?: 'input' extends keyof IO
19+
? 'query' extends keyof IO['input']
20+
? IO['input']['query']
21+
: undefined
22+
: undefined
2123
}) => string
2224
method: string
2325
form?: boolean
2426
io: IO
2527
}
2628

27-
export type ApiEndpoints = Record<string, Endpoint<any>>
29+
export type ApiEndpoints = Record<string, Endpoint<EndpointIO>>
2830

29-
export type EndpointKeys<ROUTES extends ApiEndpoints> = keyof ROUTES
31+
export type EndpointKeys<Endpoints extends ApiEndpoints> = keyof Endpoints
3032

3133
// ===============
3234
// https://github.com/Julien-R44/tuyau/blob/main/packages/utils/src/types.ts

packages/cobalt/src/components/form/index.tsx

-42
This file was deleted.

packages/cobalt/src/components/index.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
export { HeadScript } from './head_script.js'
22

3-
export { Form } from './form/index.js'
4-
53
export { QueryLoader } from './query_loader.js'
64
export type { QueryLoaderProps } from './query_loader.js'
75

packages/gate/src/client.ts

+32-25
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
1-
import { Config, Header, Token } from './types.js'
1+
import { Header, Token } from './types.js'
22
import axios, { type AxiosRequestConfig, AxiosInstance, AxiosResponse, isAxiosError } from 'axios'
33
import qs from 'qs'
44
import { GateError } from './error.js'
5-
import { ApiEndpoints, EndpointKeys } from '@folie/blueprint-lib'
5+
import { ApiEndpoints } from '@folie/blueprint-lib'
66

77
export class Gate<const Endpoints extends ApiEndpoints> {
88
#axios: AxiosInstance
99

10-
#config: Config<Endpoints>
10+
#endpoints: Endpoints
11+
#baseURL: string
1112

12-
constructor(config: Config<Endpoints>) {
13-
this.#config = config
13+
#token?: Token
14+
#header?: Header
15+
16+
constructor(config: { baseURL: URL; endpoints: Endpoints; token?: Token; header?: Header }) {
17+
this.#endpoints = config.endpoints
18+
this.#baseURL = config.baseURL.origin
19+
20+
if (config.token) {
21+
this.#token = config.token
22+
}
23+
24+
if (config.header) {
25+
this.#header = config.header
26+
}
1427

1528
this.#axios = axios.create({
16-
baseURL: this.#config.baseURL.origin,
29+
baseURL: config.baseURL.origin,
1730
headers: {
1831
'Content-Type': 'application/json',
1932
},
@@ -22,15 +35,15 @@ export class Gate<const Endpoints extends ApiEndpoints> {
2235
}
2336

2437
setToken(token: Token) {
25-
this.#config.token = token
38+
this.#token = token
2639
}
2740

2841
setHeader(header: Header) {
29-
this.#config.header = header
42+
this.#header = header
3043
}
3144

3245
token(custom?: Token) {
33-
const target = custom || this.#config.token
46+
const target = custom || this.#token
3447

3548
if (typeof target === 'string') {
3649
return target
@@ -42,7 +55,7 @@ export class Gate<const Endpoints extends ApiEndpoints> {
4255
}
4356

4457
header(custom?: Header) {
45-
const target = custom || this.#config.header
58+
const target = custom || this.#header
4659

4760
if (typeof target === 'object') {
4861
return target
@@ -54,7 +67,7 @@ export class Gate<const Endpoints extends ApiEndpoints> {
5467
}
5568

5669
async #call<
57-
EK extends EndpointKeys<Endpoints>,
70+
EK extends keyof Endpoints,
5871
EP extends Endpoints[EK],
5972
IN extends EP['io']['input'],
6073
OUT extends EP['io']['output'],
@@ -67,7 +80,7 @@ export class Gate<const Endpoints extends ApiEndpoints> {
6780
}
6881
): Promise<OUT> {
6982
try {
70-
const endpoint = this.#config.endpoints[endpointKey]
83+
const endpoint = this.#endpoints[endpointKey]
7184

7285
const [token, header] = await Promise.all([
7386
this.token(options?.token),
@@ -110,7 +123,7 @@ export class Gate<const Endpoints extends ApiEndpoints> {
110123
}
111124
}
112125

113-
async #safeCall<EK extends EndpointKeys<Endpoints>, EP extends Endpoints[EK]>(
126+
async #safeCall<EK extends keyof Endpoints, EP extends Endpoints[EK]>(
114127
endpointKey: EK,
115128
input: EP['io']['input']
116129
): Promise<[EP['io']['output'], null] | [null, GateError]> {
@@ -127,23 +140,17 @@ export class Gate<const Endpoints extends ApiEndpoints> {
127140
}
128141
}
129142

130-
endpoint<RK extends EndpointKeys<Endpoints>>(endpointKey: RK) {
143+
endpoint<EK extends keyof Endpoints>(endpointKey: EK) {
131144
return {
132-
call: (input: Endpoints[RK]['io']['input']) => this.#call(endpointKey, input),
133-
safeCall: (input: Endpoints[RK]['io']['input']) => this.#safeCall(endpointKey, input),
145+
call: (input: Endpoints[EK]['io']['input']) => this.#call(endpointKey, input),
146+
safeCall: (input: Endpoints[EK]['io']['input']) => this.#safeCall(endpointKey, input),
134147
} as const
135148
}
136149

137-
url<EK extends EndpointKeys<Endpoints>, EP extends Endpoints[EK]>(
150+
url<EK extends keyof Endpoints, EP extends Endpoints[EK]>(
138151
endpointKey: EK,
139-
options?: {
140-
params?: NonNullable<EP['io']['input']>['params']
141-
query?: NonNullable<EP['io']['input']>['query']
142-
}
152+
options?: Parameters<EP['url']>[0]
143153
) {
144-
return new URL(
145-
this.#config.endpoints[endpointKey].url({ params: options?.params, query: options?.query }),
146-
this.#config.baseURL
147-
)
154+
return new URL(this.#endpoints[endpointKey].url(options), this.#baseURL)
148155
}
149156
}

packages/gate/src/types.ts

-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
import { ApiEndpoints } from '@folie/blueprint-lib'
2-
31
export type MaybePromise<T> = T | Promise<T>
42

53
export type Token = string | (() => MaybePromise<string | null>)
64

75
export type Header = Record<string, string> | (() => MaybePromise<Record<string, string> | null>)
8-
9-
export type Config<Endpoints extends ApiEndpoints> = {
10-
baseURL: URL
11-
endpoints: Endpoints
12-
token?: Token
13-
header?: Header
14-
}
File renamed without changes.

packages/cobalt-next/index.ts renamed to packages/gate_next/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
|
88
*/
99

10-
export { CobaltNextServer } from './src/server.js'
11-
export { CobaltNextClient } from './src/client.js'
10+
export { GateNextServer } from './src/server.js'
11+
export { GateNextClient } from './src/client.js'
1212
export { NextServerError } from './src/next_server_error.js'

packages/cobalt-next/package.json renamed to packages/gate_next/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@folie/cobalt-next",
2+
"name": "@folie/gate-next",
33
"description": "",
44
"version": "0.0.0",
55
"engines": {

packages/cobalt-next/src/client.ts renamed to packages/gate_next/src/client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from 'cookies-next'
66
import { useRouter } from 'next/router.js'
77

8-
export class CobaltNextClient<
8+
export class GateNextClient<
99
CookieKeys extends Record<string, string>,
1010
ParamKeys extends readonly [...string[]],
1111
> {

packages/cobalt-next/src/server.ts renamed to packages/gate_next/src/server.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ApiEndpoints, EndpointKeys } from '@folie/blueprint-lib'
66
import { CheckpointParams } from './types.js'
77
import { NextServerError } from './next_server_error.js'
88

9-
export class CobaltNextServer<
9+
export class GateNextServer<
1010
const Endpoints extends ApiEndpoints,
1111
SessionEndpointKey extends EndpointKeys<Endpoints>,
1212
> {
@@ -100,7 +100,7 @@ export class CobaltNextServer<
100100
api: Gate<Endpoints>
101101
}) => Promise<GetServerSidePropsResult<T>>,
102102
options?: {
103-
checkpointCondition?:
103+
checkpoint?:
104104
| ((params: {
105105
ctx: GetServerSidePropsContext
106106
session: Endpoints[SessionEndpointKey]['io']['output'] | null
@@ -116,9 +116,9 @@ export class CobaltNextServer<
116116
deleteCookie(this.sessionConfig.cookie, ctx)
117117
}
118118

119-
if (options?.checkpointCondition) {
120-
if (typeof options?.checkpointCondition === 'function') {
121-
const condition = options?.checkpointCondition({ ctx, session })
119+
if (options?.checkpoint) {
120+
if (typeof options?.checkpoint === 'function') {
121+
const condition = options?.checkpoint({ ctx, session })
122122

123123
if (condition.allow === false) {
124124
return {
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

packages/gate-tan/package.json renamed to packages/gate_tan/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"exports": {
1515
".": "./build/index.js",
16-
"./components": "./build/components/index.js"
16+
"./components": "./build/src/components/index.js"
1717
},
1818
"scripts": {
1919
"clean": "del-cli build",

packages/gate-tan/src/tan.ts renamed to packages/gate_tan/src/tan.ts

+14-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApiEndpoints, EndpointKeys } from '@folie/blueprint-lib'
1+
import { ApiEndpoints } from '@folie/blueprint-lib'
22
import { Gate } from '@folie/gate'
33
import {
44
UndefinedInitialDataOptions,
@@ -32,19 +32,16 @@ export class GateTan<const Endpoints extends ApiEndpoints> {
3232
this.notification = params.notification
3333
}
3434

35-
queryKey = <EK extends EndpointKeys<Endpoints>, EP extends Endpoints[EK]['io']>(params: {
36-
endpoint: EK
37-
options?: {
38-
params?: NonNullable<EP['input']>['params']
39-
query?: NonNullable<EP['input']>['query']
40-
}
41-
}) => {
42-
const endpoint = this.endpoints[params.endpoint]
35+
queryKey = <EK extends keyof Endpoints, EP extends Endpoints[EK]>(
36+
endpointKey: EK,
37+
options?: Parameters<EP['url']>[0]
38+
) => {
39+
const endpoint = this.endpoints[endpointKey]
4340

44-
return [endpoint.method, endpoint.url(params.options)]
41+
return [endpoint.method, endpoint.url(options)]
4542
}
4643

47-
useQuery = <EK extends EndpointKeys<Endpoints>, EP extends Endpoints[EK]['io']>(
44+
useQuery = <EK extends keyof Endpoints, EP extends Endpoints[EK]['io']>(
4845
params: {
4946
endpoint: EK
5047
input: EP['input']
@@ -57,20 +54,17 @@ export class GateTan<const Endpoints extends ApiEndpoints> {
5754
...rest,
5855

5956
// Permanent
60-
queryKey: this.queryKey({
61-
endpoint: endpoint,
62-
options: {
63-
params: input?.params,
64-
query: input?.query,
65-
},
57+
queryKey: this.queryKey(endpoint, {
58+
params: input?.params,
59+
query: input?.query,
6660
}),
6761
queryFn: () => this.gate.endpoint(endpoint).call(input),
6862
})
6963

7064
return internalQuery
7165
}
7266

73-
useMutation = <EK extends EndpointKeys<Endpoints>, EP extends Endpoints[EK]['io']>(
67+
useMutation = <EK extends keyof Endpoints, EP extends Endpoints[EK]['io']>(
7468
params: CobaltUseMutationParams<Endpoints, EK, EP>
7569
) => {
7670
const { endpoint, form, onSuccess, onError, ...rest } = params
@@ -124,7 +118,7 @@ export class GateTan<const Endpoints extends ApiEndpoints> {
124118
return internalMutation
125119
}
126120

127-
useList = <EK extends EndpointKeys<Endpoints>, EP extends Endpoints[EK]['io']>(
121+
useList = <EK extends keyof Endpoints, EP extends Endpoints[EK]['io']>(
128122
params: {
129123
endpoint: EK
130124
input?: EP['input']
@@ -152,7 +146,7 @@ export class GateTan<const Endpoints extends ApiEndpoints> {
152146
return [internalQueryCall, [internalBody, setInternalBody]] as const
153147
}
154148

155-
useForm = <EK extends EndpointKeys<Endpoints>, EP extends Endpoints[EK]['io']>(
149+
useForm = <EK extends keyof Endpoints, EP extends Endpoints[EK]['io']>(
156150
params: UseFormInput<NonNullable<EP['input']>> & {
157151
endpoint: EK
158152

File renamed without changes.
File renamed without changes.

playground/frontend/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"@folie/cobalt": "workspace:*",
1919
"@folie/cobalt-animation": "workspace:*",
2020
"@folie/gate": "workspace:*",
21+
"@folie/gate-tan": "workspace:*",
22+
"@folie/gate-next": "workspace:*",
2123
"@folie/lib": "workspace:*",
2224
"@mantine/core": "^7.16.1",
2325
"@mantine/dates": "^7.16.1",

0 commit comments

Comments
 (0)