-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from vtex-apps/feature/B2BTEAM-1286-add-token-…
…validation feat: add token validation and set token in graphql clients
- Loading branch information
Showing
13 changed files
with
219 additions
and
145 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
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
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,38 +1,11 @@ | ||
import type { InstanceOptions, IOContext } from '@vtex/api' | ||
import { AppClient, GraphQLClient } from '@vtex/api' | ||
import { AppGraphQLClient } from '@vtex/api' | ||
|
||
export default class Organizations extends AppClient { | ||
protected graphql: GraphQLClient | ||
import { getTokenToHeader } from './index' | ||
|
||
export default class Organizations extends AppGraphQLClient { | ||
constructor(ctx: IOContext, options?: InstanceOptions) { | ||
super('[email protected]', ctx, options) | ||
this.graphql = new GraphQLClient(this.http) | ||
} | ||
|
||
public getOrganizationIDs = async (search: string): Promise<any> => { | ||
const graphQLQuery = `query GetOrganizations($search: String!) { | ||
getOrganizations(search: $search) { | ||
data { | ||
id | ||
} | ||
} | ||
}` | ||
|
||
return this.graphql.query( | ||
{ | ||
extensions: { | ||
persistedQuery: { | ||
provider: '[email protected]', | ||
sender: '[email protected]', | ||
}, | ||
}, | ||
query: graphQLQuery, | ||
variables: { | ||
search, | ||
}, | ||
}, | ||
{ url: '/graphql' } | ||
) | ||
super('[email protected]', ctx, options) | ||
} | ||
|
||
public getOrganizationById = async (id: string): Promise<any> => { | ||
|
@@ -43,47 +16,13 @@ export default class Organizations extends AppClient { | |
} | ||
` | ||
|
||
return this.graphql.query( | ||
{ | ||
extensions: { | ||
persistedQuery: { | ||
provider: '[email protected]', | ||
sender: '[email protected]', | ||
}, | ||
}, | ||
query: graphQLQuery, | ||
variables: { | ||
id, | ||
}, | ||
return this.query({ | ||
extensions: this.getPersistedQuery(), | ||
query: graphQLQuery, | ||
variables: { | ||
id, | ||
}, | ||
{ url: '/graphql' } | ||
) | ||
} | ||
|
||
public getCostCenterIDs = async (search: string): Promise<any> => { | ||
const graphQLQuery = `query GetCostCenters($search: String!) { | ||
getCostCenters(search: $search) { | ||
data { | ||
id | ||
} | ||
} | ||
}` | ||
|
||
return this.graphql.query( | ||
{ | ||
extensions: { | ||
persistedQuery: { | ||
provider: '[email protected]', | ||
sender: '[email protected]', | ||
}, | ||
}, | ||
query: graphQLQuery, | ||
variables: { | ||
search, | ||
}, | ||
}, | ||
{ url: '/graphql' } | ||
) | ||
}) | ||
} | ||
|
||
public getCostCenterById = async (id: string): Promise<any> => { | ||
|
@@ -94,20 +33,43 @@ export default class Organizations extends AppClient { | |
} | ||
` | ||
|
||
return this.query({ | ||
extensions: this.getPersistedQuery(), | ||
query: graphQLQuery, | ||
variables: { | ||
id, | ||
}, | ||
}) | ||
} | ||
|
||
private getPersistedQuery = () => { | ||
return { | ||
persistedQuery: { | ||
provider: '[email protected]', | ||
sender: '[email protected]', | ||
}, | ||
} | ||
} | ||
|
||
private query = async (param: { | ||
query: string | ||
variables: any | ||
extensions: any | ||
}): Promise<any> => { | ||
const { query, variables, extensions } = param | ||
|
||
return this.graphql.query( | ||
{ | ||
extensions: { | ||
persistedQuery: { | ||
provider: '[email protected]', | ||
sender: '[email protected]', | ||
}, | ||
}, | ||
query: graphQLQuery, | ||
variables: { | ||
id, | ||
}, | ||
extensions, | ||
query, | ||
variables, | ||
}, | ||
{ url: '/graphql' } | ||
{ | ||
headers: getTokenToHeader(this.context), | ||
params: { | ||
locale: this.context.locale, | ||
}, | ||
} | ||
) | ||
} | ||
} |
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,6 +1,8 @@ | ||
import type { InstanceOptions, IOContext } from '@vtex/api' | ||
import { AppGraphQLClient } from '@vtex/api' | ||
|
||
import { getTokenToHeader } from './index' | ||
|
||
export const QUERIES = { | ||
getPermission: `query permissions { | ||
checkUserPermission { | ||
|
@@ -60,35 +62,19 @@ export default class StorefrontPermissions extends AppGraphQLClient { | |
} | ||
|
||
public checkUserPermission = async (): Promise<any> => { | ||
return this.graphql.query( | ||
{ | ||
extensions: { | ||
persistedQuery: { | ||
provider: '[email protected]', | ||
sender: '[email protected]', | ||
}, | ||
}, | ||
query: QUERIES.getPermission, | ||
variables: {}, | ||
}, | ||
{} | ||
) | ||
return this.query({ | ||
extensions: this.getPersistedQuery(), | ||
query: QUERIES.getPermission, | ||
variables: {}, | ||
}) | ||
} | ||
|
||
public listRoles = async (): Promise<any> => { | ||
return this.graphql.query( | ||
{ | ||
extensions: { | ||
persistedQuery: { | ||
provider: '[email protected]', | ||
sender: '[email protected]', | ||
}, | ||
}, | ||
query: QUERIES.listRoles, | ||
variables: {}, | ||
}, | ||
{} | ||
) | ||
return this.query({ | ||
extensions: this.getPersistedQuery(), | ||
query: QUERIES.listRoles, | ||
variables: {}, | ||
}) | ||
} | ||
|
||
public listUsers = async ({ | ||
|
@@ -98,21 +84,44 @@ export default class StorefrontPermissions extends AppGraphQLClient { | |
roleId: string | ||
organizationId?: string | ||
}): Promise<any> => { | ||
return this.query({ | ||
extensions: this.getPersistedQuery(), | ||
query: QUERIES.listUsers, | ||
variables: { | ||
roleId, | ||
...(organizationId && { organizationId }), | ||
}, | ||
}) | ||
} | ||
|
||
private getPersistedQuery = () => { | ||
return { | ||
persistedQuery: { | ||
provider: '[email protected]', | ||
sender: '[email protected]', | ||
}, | ||
} | ||
} | ||
|
||
private query = async (param: { | ||
query: string | ||
variables: any | ||
extensions: any | ||
}): Promise<any> => { | ||
const { query, variables, extensions } = param | ||
|
||
return this.graphql.query( | ||
{ | ||
extensions: { | ||
persistedQuery: { | ||
provider: '[email protected]', | ||
sender: '[email protected]', | ||
}, | ||
}, | ||
query: QUERIES.listUsers, | ||
variables: { | ||
roleId, | ||
...(organizationId && { organizationId }), | ||
}, | ||
extensions, | ||
query, | ||
variables, | ||
}, | ||
{} | ||
{ | ||
headers: getTokenToHeader(this.context), | ||
params: { | ||
locale: this.context.locale, | ||
}, | ||
} | ||
) | ||
} | ||
} |
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,13 +1,15 @@ | ||
import { AuditAccess } from './directives/auditAccess' | ||
import { CheckAdminAccess } from './directives/checkAdminAccess' | ||
import { CheckUserAccess } from './directives/checkUserAccess' | ||
import { WithPermissions } from './directives/withPermissions' | ||
import { WithSession } from './directives/withSession' | ||
import { WithSegment } from './directives/withSegment' | ||
import { CheckAdminAccess } from './directives/checkAdminAccess' | ||
import { AuditAccess } from './directives/auditAccess' | ||
import { WithSession } from './directives/withSession' | ||
|
||
export const schemaDirectives = { | ||
withPermissions: WithPermissions as any, | ||
withSession: WithSession as any, | ||
withSegment: WithSegment as any, | ||
checkAdminAccess: CheckAdminAccess as any, | ||
auditAccess: AuditAccess as any, | ||
checkUserAccess: CheckUserAccess as any, | ||
} |
Oops, something went wrong.