aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sra-report/src/postman_environment_factory.ts
blob: a8753d9ff0a8277495246ddc1617a97777aa8737 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { SignedOrder, ZeroEx } from '0x.js';
import { Schema, schemas as schemasByName } from '@0xproject/json-schemas';
import * as _ from 'lodash';

import * as kovanTokensEnvironmentJSON from '../postman_configs/environments/kovan_tokens.postman_environment.json';
import * as mainnetTokensEnvironmentJSON from '../postman_configs/environments/mainnet_tokens.postman_environment.json';

interface EnvironmentValue {
    key: string;
    value: string;
    enabled: boolean;
    type: string;
}

export const postmanEnvironmentFactory = {
    createGlobalEnvironment(url: string, order: SignedOrder) {
        const schemas: Schema[] = _.values(schemasByName);
        const schemaEnvironmentValues = _.compact(
            _.map(schemas, (schema: Schema) => {
                if (_.isUndefined(schema.id)) {
                    return undefined;
                } else {
                    const schemaKey = convertSchemaIdToKey(schema.id);
                    const stringifiedSchema = JSON.stringify(schema);
                    const schemaEnvironmentValue = createEnvironmentValue(schemaKey, stringifiedSchema);
                    return schemaEnvironmentValue;
                }
            }),
        );
        const schemaKeys = _.map(schemaEnvironmentValues, (environmentValue: EnvironmentValue) => {
            return environmentValue.key;
        });
        const allEnvironmentValues = _.concat(
            schemaEnvironmentValues,
            createEnvironmentValue('schemaKeys', JSON.stringify(schemaKeys)),
            createEnvironmentValue('url', url),
            createEnvironmentValue('order', JSON.stringify(order)),
            createEnvironmentValue('orderMaker', order.maker),
            createEnvironmentValue('orderTaker', order.taker),
            createEnvironmentValue('orderFeeRecipient', order.feeRecipient),
            createEnvironmentValue('orderHash', ZeroEx.getOrderHashHex(order)),
        );
        const environment = {
            values: allEnvironmentValues,
        };
        return environment;
    },
    createNetworkEnvironment(networkId: number) {
        switch (networkId) {
            case 1:
                return mainnetTokensEnvironmentJSON;
            case 42:
                return kovanTokensEnvironmentJSON;
            default:
                return {};
        }
    },
};
function convertSchemaIdToKey(schemaId: string) {
    let result = schemaId;
    if (_.startsWith(result, '/')) {
        result = result.substr(1);
    }
    result = `${result}Schema`;
    return result;
}
function createEnvironmentValue(key: string, value: string) {
    return {
        key,
        value,
        enabled: true,
        type: 'text',
    };
}