-
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 #1 from vtex-apps/export-category
Export category
- Loading branch information
Showing
22 changed files
with
1,981 additions
and
36 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,8 @@ | ||
{ | ||
"extends": "vtex", | ||
"root": true, | ||
"env": { | ||
"node": true, | ||
"es6": true | ||
} | ||
} |
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,3 @@ | ||
type Query { | ||
categoryTranslations(locale: String!, active: Boolean): [Category] | ||
} |
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,7 @@ | ||
type Category { | ||
id: String | ||
name: String | ||
title: String | ||
description: String | ||
locale: 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
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,85 @@ | ||
import { AppGraphQLClient, InstanceOptions, IOContext } from '@vtex/api' | ||
|
||
import { statusToError } from '../utils' | ||
|
||
const CATALOG_GRAPHQL_APP = '[email protected]' | ||
|
||
const CATEGORIES_QUERY = ` | ||
query GetCategoriesId ($active: Boolean, $page: Int!) { | ||
categories(term:"*", page: $page, pageSize: 50, active: $active) { | ||
items { | ||
id | ||
} | ||
paging { | ||
pages | ||
} | ||
} | ||
} | ||
` | ||
|
||
const GET_TRANSLATION_QUERY = ` | ||
query getTranslation($id:ID!) { | ||
category(id: $id) { | ||
id | ||
name | ||
title | ||
description | ||
} | ||
} | ||
` | ||
|
||
export class Catalog extends AppGraphQLClient { | ||
constructor(ctx: IOContext, opts?: InstanceOptions) { | ||
super(CATALOG_GRAPHQL_APP, ctx, opts) | ||
} | ||
|
||
public getCategoriesId = async (active = true) => { | ||
try { | ||
const response = await this.getCategoriesIdPerPage({ active, page: 1 }) | ||
const { | ||
items, | ||
paging: { pages }, | ||
} = (response.data as CategoryIdsResponse).categories | ||
const collectItems = items | ||
const responsePromises = [] | ||
|
||
for (let i = 2; i <= pages; i++) { | ||
const promise = this.getCategoriesIdPerPage({ active, page: i }) | ||
responsePromises.push(promise) | ||
} | ||
|
||
const resolvedPromises = await Promise.all(responsePromises) | ||
|
||
const flattenResponse = resolvedPromises.reduce((acc, curr) => { | ||
return [...acc, ...(curr.data as CategoryIdsResponse).categories.items] | ||
}, collectItems) | ||
|
||
return flattenResponse | ||
} catch (error) { | ||
return statusToError(error) | ||
} | ||
} | ||
|
||
private getCategoriesIdPerPage = ({ | ||
active = true, | ||
page, | ||
}: { | ||
active: boolean | ||
page: number | ||
}) => | ||
this.graphql.query<CategoryIdsResponse, { active: boolean; page: number }>({ | ||
query: CATEGORIES_QUERY, | ||
variables: { | ||
active, | ||
page, | ||
}, | ||
}) | ||
|
||
public getTranslation = (id: string) => | ||
this.graphql.query<TranslationResponse, { id: string }>({ | ||
query: GET_TRANSLATION_QUERY, | ||
variables: { | ||
id, | ||
}, | ||
}) | ||
} |
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,9 @@ | ||
import { IOClients } from '@vtex/api' | ||
|
||
import { Catalog } from './catalog' | ||
|
||
export class Clients extends IOClients { | ||
public get catalog() { | ||
return this.getOrSet('catalog', Catalog) | ||
} | ||
} |
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,36 @@ | ||
import { | ||
ParamsContext, | ||
RecorderState, | ||
Service, | ||
ServiceContext, | ||
} from '@vtex/api' | ||
|
||
import { Clients } from './clients' | ||
import { resolvers, queries } from './resolvers' | ||
|
||
declare global { | ||
type Context = ServiceContext<Clients, State> | ||
|
||
interface State extends RecorderState { | ||
locale: string | ||
} | ||
} | ||
|
||
export default new Service<Clients, State, ParamsContext>({ | ||
clients: { | ||
implementation: Clients, | ||
options: { | ||
default: { | ||
retries: 2, | ||
}, | ||
}, | ||
}, | ||
graphql: { | ||
resolvers: { | ||
...resolvers, | ||
Query: { | ||
...queries, | ||
}, | ||
}, | ||
}, | ||
}) |
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,13 @@ | ||
{ | ||
"name": "vtex.admin-catalog-translation", | ||
"version": "0.0.2", | ||
"main": "index.ts", | ||
"license": "UNLICENSED", | ||
"dependencies": { | ||
"@vtex/api": "6.41.0" | ||
}, | ||
"devDependencies": { | ||
"@vtex/tsconfig": "^0.5.6", | ||
"typescript": "3.9.7" | ||
} | ||
} |
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,48 @@ | ||
import { statusToError } from '../utils' | ||
|
||
export const Category = { | ||
locale: ( | ||
_root: ResolvedPromise<TranslationResponse>, | ||
_args: unknown, | ||
ctx: Context | ||
) => { | ||
return ctx.state.locale | ||
}, | ||
name: (root: ResolvedPromise<TranslationResponse>) => root.data.category.name, | ||
title: (root: ResolvedPromise<TranslationResponse>) => | ||
root.data.category.title, | ||
description: (root: ResolvedPromise<TranslationResponse>) => | ||
root.data.category.description, | ||
id: (root: ResolvedPromise<TranslationResponse>) => root.data.category.id, | ||
} | ||
|
||
const categoryTranslations = async ( | ||
_root: unknown, | ||
args: { locale: string; active?: boolean }, | ||
ctx: Context | ||
) => { | ||
const { | ||
clients: { catalog }, | ||
} = ctx | ||
|
||
ctx.state.locale = args.locale | ||
|
||
try { | ||
const ids = await catalog.getCategoriesId() | ||
|
||
const translationsP = [] | ||
|
||
for (const { id } of ids) { | ||
const promise = catalog.getTranslation(id) | ||
translationsP.push(promise) | ||
} | ||
|
||
return translationsP | ||
} catch (error) { | ||
return statusToError(error) | ||
} | ||
} | ||
|
||
export const queries = { | ||
categoryTranslations, | ||
} |
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,9 @@ | ||
import { Category, queries as categoryQueries } from './category' | ||
|
||
export const queries = { | ||
...categoryQueries, | ||
} | ||
|
||
export const resolvers = { | ||
Category, | ||
} |
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,8 @@ | ||
{ | ||
"memory": 256, | ||
"ttl": 10, | ||
"timeout": 5, | ||
"minReplicas": 2, | ||
"maxReplicas": 4, | ||
"workers": 2 | ||
} |
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,26 @@ | ||
{ | ||
"compilerOptions": { | ||
"types": ["node"], | ||
"typeRoots": ["node_modules/@types"], | ||
"target": "es2019", | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"allowJs": false, | ||
"esModuleInterop": true, | ||
"noEmitOnError": true, | ||
"noImplicitReturns": true, | ||
"noImplicitAny": true, | ||
"noImplicitThis": true, | ||
"alwaysStrict": true, | ||
"strict": true, | ||
"strictNullChecks": true, | ||
"strictFunctionTypes": true, | ||
"strictPropertyInitialization": true, | ||
"skipLibCheck": true | ||
}, | ||
"typeAcquisition": { | ||
"enable": false | ||
}, | ||
"include": ["./**/*.ts", "./typings/*.d.ts"], | ||
"exclude": ["node_modules"] | ||
} |
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,21 @@ | ||
interface CategoryIdsResponse { | ||
categories: { | ||
items: Array<{ id: string }> | ||
paging: { | ||
pages: number | ||
} | ||
} | ||
} | ||
|
||
interface TranslationResponse { | ||
category: { | ||
id: string | ||
name: string | ||
title: string | ||
description: string | ||
} | ||
} | ||
|
||
interface ResolvedPromise<Response> { | ||
data: Response | ||
} |
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,29 @@ | ||
import { AuthenticationError, ForbiddenError, UserInputError } from '@vtex/api' | ||
import type { AxiosError } from 'axios' | ||
|
||
export const statusToError = (e: AxiosError) => { | ||
if (!e.response) { | ||
throw e | ||
} | ||
|
||
const { | ||
response: { status }, | ||
} = e | ||
|
||
switch (status) { | ||
case 401: { | ||
throw new AuthenticationError(e) | ||
} | ||
|
||
case 403: { | ||
throw new ForbiddenError(e) | ||
} | ||
|
||
case 400: { | ||
throw new UserInputError(e) | ||
} | ||
|
||
default: | ||
throw new TypeError(e.message) | ||
} | ||
} |
Oops, something went wrong.