From 7747209810d58b576a9a4cc3eee20bf1f76073dd Mon Sep 17 00:00:00 2001 From: Andrey Rublev Date: Wed, 24 May 2017 16:48:16 +0200 Subject: [PATCH 1/2] added flow types for api data structures and flow-typed declarations for module --- flow-typed/api.js | 61 +++++++++++++++++++++++++++++++++++++ types/api/album.js | 13 ++++++++ types/api/attrs.js | 13 ++++++++ types/api/base.js | 8 +++++ types/api/product.js | 31 +++++++++++++++++++ types/api/sku.js | 15 +++++++++ types/api/views/album.js | 12 ++++++++ types/api/views/country.js | 13 ++++++++ types/api/views/product.js | 21 +++++++++++++ types/api/views/region.js | 7 +++++ types/api/views/taxonomy.js | 5 +++ 11 files changed, 199 insertions(+) create mode 100644 flow-typed/api.js create mode 100644 types/api/album.js create mode 100644 types/api/attrs.js create mode 100644 types/api/base.js create mode 100644 types/api/product.js create mode 100644 types/api/sku.js create mode 100644 types/api/views/album.js create mode 100644 types/api/views/country.js create mode 100644 types/api/views/product.js create mode 100644 types/api/views/region.js create mode 100644 types/api/views/taxonomy.js diff --git a/flow-typed/api.js b/flow-typed/api.js new file mode 100644 index 0000000..93350fc --- /dev/null +++ b/flow-typed/api.js @@ -0,0 +1,61 @@ + +declare module '@foxcomm/api-js' { + + declare type StringDict = {[name: string]: string}; + + type AbortablePromise = Promise & { + abort(): void; + } + + declare type AgentLike = AbortablePromise & { + get(url: string): AgentLike; + post(url: string): AgentLike; + patch(url: string): AgentLike; + delete(url: string): AgentLike; + + set(headers: StringDict): AgentLike; + withCredentials(): AgentLike; + } + + declare type RequestOptions = { + headers?: StringDict, + credentials?: string, + agent?: AgentLike; + } + + // @TODO: add subclasses + declare class ApiClass { + addresses: mixed; + auth: mixed; + creditCards: mixed; + storeCredits: mixed; + cart: mixed; + account: mixed; + orders: mixed; + reviews: mixed; + analytics: mixed; + crossSell: mixed; + + addAuth(jwt: string): ApiClass; + removeAuth(): ApiClass; + + // Returns customer id from parsed jwt string + // You can define jwt string via `addAuth` method, if there is no jwt strings method returns null. + getCustomerId(): ?number; + + setHeaders(headers: StringDict): ApiClass; + addHeaders(headers: StringDict): ApiClass; + uri(path: string): string; + queryStringToObject(qs: string): StringDict; + + request(method: string, uri: string, data: ?Object, options: ?RequestOptions): AbortablePromise; + get(uri: string, data: ?Object, options: ?Object): AbortablePromise; + post(uri: string, data: ?Object, options: ?Object): AbortablePromise; + patch(uri: string, data: ?Object, options: ?Object): AbortablePromise; + delete(uri: string, data: ?Object, options: ?Object): AbortablePromise; + } + + declare function parseError(err: mixed): Array; + + declare module.exports: typeof ApiClass; +} diff --git a/types/api/album.js b/types/api/album.js new file mode 100644 index 0000000..7ec20e6 --- /dev/null +++ b/types/api/album.js @@ -0,0 +1,13 @@ + +export type Image = { + alt: string, + src: string, + title?: string, +} + +export type Album = { + id: number, + name: string, + images: Array, + archivedAt?: string, +} diff --git a/types/api/attrs.js b/types/api/attrs.js new file mode 100644 index 0000000..7aa87df --- /dev/null +++ b/types/api/attrs.js @@ -0,0 +1,13 @@ + +export type Currency = {| + t: 'price', + v: { + currency: string, + value: number + } +|} + +export type String = {| + t: 'string', + v: string +|} diff --git a/types/api/base.js b/types/api/base.js new file mode 100644 index 0000000..b0c80ab --- /dev/null +++ b/types/api/base.js @@ -0,0 +1,8 @@ + +export type Context = {| + name: string, + attributes: { + lang: string, + modality: string, + } +|} diff --git a/types/api/product.js b/types/api/product.js new file mode 100644 index 0000000..2ed3acd --- /dev/null +++ b/types/api/product.js @@ -0,0 +1,31 @@ + +import type { Context } from './base'; +import type { String } from './attrs'; +import type { Sku } from './sku'; +import type { Album } from './album'; + +export type VariantValue = { + id: number, + name: string, + swatch: string, + skuCodes: Array, +} + +export type Variant = { + attributes: { + name: String, + }, + values: Array, +} + +export type Product = { + id: number, + context: Context, + albums: Array, + skus: Array, + attributes: { + name: String, + description: String, + }, + variants: Array, +} diff --git a/types/api/sku.js b/types/api/sku.js new file mode 100644 index 0000000..a388c4e --- /dev/null +++ b/types/api/sku.js @@ -0,0 +1,15 @@ + +import type { Context } from './base'; +import type { Currency, String } from './attrs'; +import type { Album } from './album'; + +export type Sku = { + id: number, + context: Context, + attributes: { + code: String, + salePrice: Currency, + retailPrice: Currency, + }, + albums: Array +}; diff --git a/types/api/views/album.js b/types/api/views/album.js new file mode 100644 index 0000000..48c209e --- /dev/null +++ b/types/api/views/album.js @@ -0,0 +1,12 @@ + +export type Image = { + alt?: string, + src: string, + title?: string, + baseurl?: string, +}; + +export type Album = { + name: string, + images: Array, +}; diff --git a/types/api/views/country.js b/types/api/views/country.js new file mode 100644 index 0000000..5d89ca6 --- /dev/null +++ b/types/api/views/country.js @@ -0,0 +1,13 @@ + +export type Country = { + id: number, + name: string, + alpha2: string, + alpha3: string, + code: string, + continent: string, + currency: string, + uses_postal_code: boolean, + is_billable: boolean, + is_shippable: boolean, +} diff --git a/types/api/views/product.js b/types/api/views/product.js new file mode 100644 index 0000000..65cc7c5 --- /dev/null +++ b/types/api/views/product.js @@ -0,0 +1,21 @@ + +import type { Album } from './album'; +import type { Taxonomy } from './taxonomy'; + +export type Product = { + id: number, + productId: number, + slug: ?string, + context: string, + currency: string, + title: string, + description: ?string, + salePrice: string, + retailPrice: string, + currency: string, + albums: ?Array | Object, + skus: Array, + tags?: Array, + scope?: string, + taxonomies: Array, +}; diff --git a/types/api/views/region.js b/types/api/views/region.js new file mode 100644 index 0000000..b299304 --- /dev/null +++ b/types/api/views/region.js @@ -0,0 +1,7 @@ + +export type Region = { + id: number, + name: string, + abbreviation: string, + countryId: number, +} diff --git a/types/api/views/taxonomy.js b/types/api/views/taxonomy.js new file mode 100644 index 0000000..352218a --- /dev/null +++ b/types/api/views/taxonomy.js @@ -0,0 +1,5 @@ + +export type Taxonomy = { + taxonomy: string, + taxons: Array> +} From 7e204fb3b0343b332e05a3f33ee53da6ceaa5cd1 Mon Sep 17 00:00:00 2001 From: Andrey Rublev Date: Wed, 24 May 2017 22:35:55 +0200 Subject: [PATCH 2/2] semicolons --- flow-typed/api.js | 9 ++++----- types/api/album.js | 4 ++-- types/api/attrs.js | 4 ++-- types/api/base.js | 2 +- types/api/product.js | 6 +++--- types/api/sku.js | 2 +- types/api/views/country.js | 2 +- types/api/views/region.js | 2 +- types/api/views/taxonomy.js | 2 +- 9 files changed, 16 insertions(+), 17 deletions(-) diff --git a/flow-typed/api.js b/flow-typed/api.js index 93350fc..f624f8f 100644 --- a/flow-typed/api.js +++ b/flow-typed/api.js @@ -1,11 +1,10 @@ declare module '@foxcomm/api-js' { - declare type StringDict = {[name: string]: string}; - type AbortablePromise = Promise & { + declare type AbortablePromise = Promise & { abort(): void; - } + }; declare type AgentLike = AbortablePromise & { get(url: string): AgentLike; @@ -15,13 +14,13 @@ declare module '@foxcomm/api-js' { set(headers: StringDict): AgentLike; withCredentials(): AgentLike; - } + }; declare type RequestOptions = { headers?: StringDict, credentials?: string, agent?: AgentLike; - } + }; // @TODO: add subclasses declare class ApiClass { diff --git a/types/api/album.js b/types/api/album.js index 7ec20e6..20f6b3b 100644 --- a/types/api/album.js +++ b/types/api/album.js @@ -3,11 +3,11 @@ export type Image = { alt: string, src: string, title?: string, -} +}; export type Album = { id: number, name: string, images: Array, archivedAt?: string, -} +}; diff --git a/types/api/attrs.js b/types/api/attrs.js index 7aa87df..ec38e42 100644 --- a/types/api/attrs.js +++ b/types/api/attrs.js @@ -5,9 +5,9 @@ export type Currency = {| currency: string, value: number } -|} +|}; export type String = {| t: 'string', v: string -|} +|}; diff --git a/types/api/base.js b/types/api/base.js index b0c80ab..c0e20c0 100644 --- a/types/api/base.js +++ b/types/api/base.js @@ -5,4 +5,4 @@ export type Context = {| lang: string, modality: string, } -|} +|}; diff --git a/types/api/product.js b/types/api/product.js index 2ed3acd..26b4fa1 100644 --- a/types/api/product.js +++ b/types/api/product.js @@ -9,14 +9,14 @@ export type VariantValue = { name: string, swatch: string, skuCodes: Array, -} +}; export type Variant = { attributes: { name: String, }, values: Array, -} +}; export type Product = { id: number, @@ -28,4 +28,4 @@ export type Product = { description: String, }, variants: Array, -} +}; diff --git a/types/api/sku.js b/types/api/sku.js index a388c4e..90cadcb 100644 --- a/types/api/sku.js +++ b/types/api/sku.js @@ -11,5 +11,5 @@ export type Sku = { salePrice: Currency, retailPrice: Currency, }, - albums: Array + albums: Array, }; diff --git a/types/api/views/country.js b/types/api/views/country.js index 5d89ca6..4360b33 100644 --- a/types/api/views/country.js +++ b/types/api/views/country.js @@ -10,4 +10,4 @@ export type Country = { uses_postal_code: boolean, is_billable: boolean, is_shippable: boolean, -} +}; diff --git a/types/api/views/region.js b/types/api/views/region.js index b299304..4843e68 100644 --- a/types/api/views/region.js +++ b/types/api/views/region.js @@ -4,4 +4,4 @@ export type Region = { name: string, abbreviation: string, countryId: number, -} +}; diff --git a/types/api/views/taxonomy.js b/types/api/views/taxonomy.js index 352218a..5f0e22d 100644 --- a/types/api/views/taxonomy.js +++ b/types/api/views/taxonomy.js @@ -2,4 +2,4 @@ export type Taxonomy = { taxonomy: string, taxons: Array> -} +};