Skip to content

Commit b6d78cb

Browse files
authored
Merge pull request #44 from FoxComm/api-types
added flow types for api data structures and flow-typed declarations …
2 parents eb7352c + 7e204fb commit b6d78cb

11 files changed

Lines changed: 198 additions & 0 deletions

File tree

flow-typed/api.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
declare module '@foxcomm/api-js' {
3+
declare type StringDict = {[name: string]: string};
4+
5+
declare type AbortablePromise = Promise & {
6+
abort(): void;
7+
};
8+
9+
declare type AgentLike = AbortablePromise & {
10+
get(url: string): AgentLike;
11+
post(url: string): AgentLike;
12+
patch(url: string): AgentLike;
13+
delete(url: string): AgentLike;
14+
15+
set(headers: StringDict): AgentLike;
16+
withCredentials(): AgentLike;
17+
};
18+
19+
declare type RequestOptions = {
20+
headers?: StringDict,
21+
credentials?: string,
22+
agent?: AgentLike;
23+
};
24+
25+
// @TODO: add subclasses
26+
declare class ApiClass {
27+
addresses: mixed;
28+
auth: mixed;
29+
creditCards: mixed;
30+
storeCredits: mixed;
31+
cart: mixed;
32+
account: mixed;
33+
orders: mixed;
34+
reviews: mixed;
35+
analytics: mixed;
36+
crossSell: mixed;
37+
38+
addAuth(jwt: string): ApiClass;
39+
removeAuth(): ApiClass;
40+
41+
// Returns customer id from parsed jwt string
42+
// You can define jwt string via `addAuth` method, if there is no jwt strings method returns null.
43+
getCustomerId(): ?number;
44+
45+
setHeaders(headers: StringDict): ApiClass;
46+
addHeaders(headers: StringDict): ApiClass;
47+
uri(path: string): string;
48+
queryStringToObject(qs: string): StringDict;
49+
50+
request(method: string, uri: string, data: ?Object, options: ?RequestOptions): AbortablePromise;
51+
get(uri: string, data: ?Object, options: ?Object): AbortablePromise;
52+
post(uri: string, data: ?Object, options: ?Object): AbortablePromise;
53+
patch(uri: string, data: ?Object, options: ?Object): AbortablePromise;
54+
delete(uri: string, data: ?Object, options: ?Object): AbortablePromise;
55+
}
56+
57+
declare function parseError(err: mixed): Array<string>;
58+
59+
declare module.exports: typeof ApiClass;
60+
}

types/api/album.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
export type Image = {
3+
alt: string,
4+
src: string,
5+
title?: string,
6+
};
7+
8+
export type Album = {
9+
id: number,
10+
name: string,
11+
images: Array<Image>,
12+
archivedAt?: string,
13+
};

types/api/attrs.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
export type Currency = {|
3+
t: 'price',
4+
v: {
5+
currency: string,
6+
value: number
7+
}
8+
|};
9+
10+
export type String = {|
11+
t: 'string',
12+
v: string
13+
|};

types/api/base.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
export type Context = {|
3+
name: string,
4+
attributes: {
5+
lang: string,
6+
modality: string,
7+
}
8+
|};

types/api/product.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
import type { Context } from './base';
3+
import type { String } from './attrs';
4+
import type { Sku } from './sku';
5+
import type { Album } from './album';
6+
7+
export type VariantValue = {
8+
id: number,
9+
name: string,
10+
swatch: string,
11+
skuCodes: Array<string>,
12+
};
13+
14+
export type Variant = {
15+
attributes: {
16+
name: String,
17+
},
18+
values: Array<VariantValue>,
19+
};
20+
21+
export type Product = {
22+
id: number,
23+
context: Context,
24+
albums: Array<Album>,
25+
skus: Array<Sku>,
26+
attributes: {
27+
name: String,
28+
description: String,
29+
},
30+
variants: Array<Variant>,
31+
};

types/api/sku.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
import type { Context } from './base';
3+
import type { Currency, String } from './attrs';
4+
import type { Album } from './album';
5+
6+
export type Sku = {
7+
id: number,
8+
context: Context,
9+
attributes: {
10+
code: String,
11+
salePrice: Currency,
12+
retailPrice: Currency,
13+
},
14+
albums: Array<Album>,
15+
};

types/api/views/album.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
export type Image = {
3+
alt?: string,
4+
src: string,
5+
title?: string,
6+
baseurl?: string,
7+
};
8+
9+
export type Album = {
10+
name: string,
11+
images: Array<Image>,
12+
};

types/api/views/country.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
export type Country = {
3+
id: number,
4+
name: string,
5+
alpha2: string,
6+
alpha3: string,
7+
code: string,
8+
continent: string,
9+
currency: string,
10+
uses_postal_code: boolean,
11+
is_billable: boolean,
12+
is_shippable: boolean,
13+
};

types/api/views/product.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
import type { Album } from './album';
3+
import type { Taxonomy } from './taxonomy';
4+
5+
export type Product = {
6+
id: number,
7+
productId: number,
8+
slug: ?string,
9+
context: string,
10+
currency: string,
11+
title: string,
12+
description: ?string,
13+
salePrice: string,
14+
retailPrice: string,
15+
currency: string,
16+
albums: ?Array<Album> | Object,
17+
skus: Array<string>,
18+
tags?: Array<string>,
19+
scope?: string,
20+
taxonomies: Array<Taxonomy>,
21+
};

types/api/views/region.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
export type Region = {
3+
id: number,
4+
name: string,
5+
abbreviation: string,
6+
countryId: number,
7+
};

0 commit comments

Comments
 (0)