-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 🎸 gql and net lib * feat: 🎸 add gql
- Loading branch information
1 parent
cb1dca3
commit 3b447a6
Showing
32 changed files
with
1,192 additions
and
153 deletions.
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 @@ | ||
**/*/graphql.d.ts |
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
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 @@ | ||
lts/* |
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
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
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 |
---|---|---|
@@ -1,18 +1,26 @@ | ||
import {createApp} from 'vue'; | ||
import type { BootstrapOption } from '@cosmic/core/parts'; | ||
import { createApp } from 'vue'; | ||
import urql from '@urql/vue'; | ||
import { gqlClientOptions } from '@cosmic/core/parts'; | ||
import { MComponent } from '@cosmic-module/core'; | ||
|
||
import { createContainer } from './ioc/index'; | ||
import App from './app.vue'; | ||
|
||
import type { BootstrapOption } from '@cosmic/core/parts'; | ||
|
||
|
||
function bootstrap(option: BootstrapOption) { | ||
const app = createApp(App); | ||
const container = createContainer({ defaultScope: 'Singleton' }); | ||
const app = createApp(App); | ||
|
||
// eslint-disable-next-line vue/component-definition-name-casing | ||
app.component('m-component', MComponent); | ||
app.use(urql, gqlClientOptions); | ||
app.provide('container', container); | ||
app.mount(option.root); | ||
|
||
} | ||
|
||
export { bootstrap }; | ||
|
||
export { interfaces as iocInterface, TOKENS as iocToken } from './ioc/index'; |
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 |
---|---|---|
@@ -1,18 +1,15 @@ | ||
import { Container } from '@cosmic/core/parts'; | ||
import type { interfaces} from 'inversify'; | ||
import { Container } from 'inversify'; | ||
import { TOKENS } from './token'; | ||
|
||
import { gqlClient } from './entity/gql'; | ||
|
||
import type { interfaces } from '@cosmic/core/parts'; | ||
import type { GqlClient, GqlClientProvider } from './interfaces'; | ||
import type { GqlClient } from './interfaces'; | ||
|
||
export function load(options: interfaces.ContainerOptions) { | ||
const container = new Container(options); | ||
|
||
// put all coupling loigc here | ||
container.bind<GqlClientProvider>(TOKENS.GqlClient).toProvider<GqlClient>(() => { | ||
return () => { | ||
return Promise.resolve({ useQuery: () => 0 }); | ||
}; | ||
}); | ||
container.bind<GqlClient>(TOKENS.GqlClient).toConstantValue(gqlClient); | ||
return container; | ||
} |
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 @@ | ||
export * as gqlClient from '@urql/vue'; |
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
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
export interface GqlClient { | ||
useQuery: () => void; | ||
} | ||
import type { gqlClient } from './entity/gql'; | ||
|
||
export type GqlClientProvider = () => Promise<GqlClient>; | ||
export type GqlClient = typeof gqlClient; |
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
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
export * from './ioc/inject'; | ||
export * from './types'; | ||
export * from './lib/observable'; | ||
export * from './lib/gql/index'; | ||
|
||
export { Container, injectable, interfaces } from 'inversify'; | ||
import { Container, inject, injectable } from 'inversify'; | ||
|
||
export const inversify = { Container, inject, injectable }; |
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
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,75 @@ | ||
/** | ||
* @author zfy<biyingshuai@gmail.com> | ||
* @description auth token | ||
*/ | ||
import { makeOperation } from '@urql/vue'; | ||
import type { Operation, CombinedError, OperationType, OperationContext } from '@urql/vue'; | ||
import { logout, get } from './user'; | ||
|
||
interface IAuthState { | ||
accessToken?: string; | ||
} | ||
|
||
interface AddParams { | ||
authState: IAuthState | null; | ||
operation: Operation; | ||
} | ||
|
||
interface GetParams { | ||
authState: IAuthState | null; | ||
operation?: Operation | null; | ||
} | ||
|
||
export function addAuthToOperation(params: AddParams) { | ||
const { authState, operation } = params || {}; | ||
const fetchOptions = operation?.context?.fetchOptions; | ||
if (!authState || !authState.accessToken) { | ||
return operation; | ||
} | ||
if (!operation) { | ||
throw Error('runtime error'); | ||
} | ||
const { kind, context } = operation; | ||
if (!kind || !context) { | ||
throw Error('runtime error'); | ||
} | ||
|
||
const options = typeof fetchOptions === 'function' ? fetchOptions() : fetchOptions || {}; | ||
|
||
return makeOperation(kind as OperationType, operation, { | ||
...context, | ||
fetchOptions: { | ||
...options, | ||
headers: { | ||
...options.headers, | ||
Authorization: `Bearer ${authState.accessToken}`, | ||
}, | ||
}, | ||
} as OperationContext); | ||
} | ||
|
||
export async function getAuth(params: GetParams) { | ||
const { authState } = params; | ||
if (!authState) { | ||
const accessToken = get().token; | ||
if (accessToken) { | ||
return { accessToken }; | ||
} | ||
// logout(); | ||
// return null; | ||
} | ||
logout(); | ||
return null; | ||
} | ||
|
||
export const didAuthError = ({ error }: { error: CombinedError }) => { | ||
return error.graphQLErrors.some(e => e.extensions?.code === 'UNAUTHENTICATED'); | ||
}; | ||
|
||
export function willAuthError({ authState }: GetParams) { | ||
if (!authState || !authState.accessToken) { | ||
return true; | ||
} | ||
// TODO: check expires | ||
return false; | ||
} |
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,10 @@ | ||
import { authExchange as exchange } from '@urql/exchange-auth'; | ||
import { getAuth, addAuthToOperation, willAuthError, didAuthError } from './auth'; | ||
|
||
// TODO: 所有模块取拿token通过user.ts | ||
export const authExchange = exchange({ | ||
getAuth, | ||
addAuthToOperation, | ||
willAuthError, | ||
didAuthError, | ||
}); |
Oops, something went wrong.