Updated login operation

This commit is contained in:
Luis Alvarez 2021-05-20 17:18:17 -05:00
parent 1086fce6bd
commit 4412d8e66c
4 changed files with 42 additions and 20 deletions

View File

@ -3,7 +3,8 @@ import type {
OperationContext, OperationContext,
OperationOptions, OperationOptions,
} from '@commerce/api/operations' } from '@commerce/api/operations'
import type { LoginMutation, LoginMutationVariables } from '../../schema' import type { LoginOperation } from '../../types/login'
import type { LoginMutation } from '../../schema'
import type { RecursivePartial } from '../utils/types' import type { RecursivePartial } from '../utils/types'
import concatHeader from '../utils/concat-cookie' import concatHeader from '../utils/concat-cookie'
import type { BigcommerceConfig, Provider } from '..' import type { BigcommerceConfig, Provider } from '..'
@ -16,24 +17,20 @@ export const loginMutation = /* GraphQL */ `
} }
` `
export type LoginResult<T extends { result?: any } = { result?: string }> = T
export type LoginVariables = LoginMutationVariables
function loginOperation({ commerce }: OperationContext<Provider>) { function loginOperation({ commerce }: OperationContext<Provider>) {
async function login(opts: { async function login(opts: {
variables: LoginVariables variables: LoginOperation['variables']
config?: BigcommerceConfig config?: BigcommerceConfig
res: ServerResponse res: ServerResponse
}): Promise<LoginResult> }): Promise<LoginOperation['data']>
async function login<T extends { result?: any }, V = any>( async function login<T extends LoginOperation>(
opts: { opts: {
variables: V variables: T['variables']
config?: BigcommerceConfig config?: BigcommerceConfig
res: ServerResponse res: ServerResponse
} & OperationOptions } & OperationOptions
): Promise<LoginResult<T>> ): Promise<T['data']>
async function login({ async function login({
query = loginMutation, query = loginMutation,
@ -42,10 +39,10 @@ function loginOperation({ commerce }: OperationContext<Provider>) {
config, config,
}: { }: {
query?: string query?: string
variables: LoginVariables variables: LoginOperation['variables']
res: ServerResponse res: ServerResponse
config?: BigcommerceConfig config?: BigcommerceConfig
}): Promise<LoginResult> { }): Promise<LoginOperation['data']> {
config = commerce.getConfig(config) config = commerce.getConfig(config)
const { data, res } = await config.fetch<RecursivePartial<LoginMutation>>( const { data, res } = await config.fetch<RecursivePartial<LoginMutation>>(

View File

@ -1 +1,8 @@
import * as Core from '@commerce/types/login'
import type { LoginMutationVariables } from '../schema'
export * from '@commerce/types/login' export * from '@commerce/types/login'
export type LoginOperation = Core.LoginOperation & {
variables: LoginMutationVariables
}

View File

@ -1,3 +1,5 @@
import type { LoginOperation } from '../types/login'
import type { GetAllPagesResult } from '../types/page'
import type { ServerResponse } from 'http' import type { ServerResponse } from 'http'
import type { APIProvider, CommerceAPI, CommerceAPICore } from '.' import type { APIProvider, CommerceAPI, CommerceAPICore } from '.'
@ -14,23 +16,34 @@ export const defaultOperations = OPERATIONS.reduce((ops, k) => {
export type AllowedOperations = typeof OPERATIONS[number] export type AllowedOperations = typeof OPERATIONS[number]
export type LoginResult<T extends { result?: any } = { result?: string }> = T
export type Operations<P extends APIProvider> = { export type Operations<P extends APIProvider> = {
login: { login: {
(opts: { <T extends LoginOperation>(opts: {
variables: any variables: T['variables']
config?: P['config'] config?: P['config']
res: ServerResponse res: ServerResponse
}): Promise<LoginResult> }): Promise<T['data']>
<T extends { result?: any }, V = any>( <T extends LoginOperation>(
opts: { opts: {
variables: V variables: T['variables']
config?: P['config'] config?: P['config']
res: ServerResponse res: ServerResponse
} & OperationOptions } & OperationOptions
): Promise<LoginResult<T>> ): Promise<T['data']>
}
getAllPages: {
(opts?: {
config?: P['config']
preview?: boolean
}): Promise<GetAllPagesResult>
<T extends GetAllPagesResult>(
opts: {
config?: P['config']
preview?: boolean
} & OperationOptions
): Promise<T>
} }
} }

View File

@ -18,3 +18,8 @@ export type LoginSchema<T extends LoginTypes = LoginTypes> = {
} }
} }
} }
export type LoginOperation = {
data: { result?: string }
variables: unknown
}