aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sra-report/src/postman_environment_factory.ts
blob: bea805ca64260b703faefc95d3cf5c16d659bbd1 (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
75
76
77
78
79
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 orderHash = ZeroEx.getOrderHashHex(order);
        const orderEnvironmentValue = createEnvironmentValue('order', JSON.stringify(order));
        const orderHashEnvironmentValue = createEnvironmentValue('orderHash', orderHash);
        const urlEnvironmentValue = createEnvironmentValue('url', url);
        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 schemaKeysEnvironmentValue = createEnvironmentValue('schemaKeys', JSON.stringify(schemaKeys));
        const environmentValues = _.concat(
            schemaEnvironmentValues,
            urlEnvironmentValue,
            schemaKeysEnvironmentValue,
            orderEnvironmentValue,
            orderHashEnvironmentValue,
        );
        const environment = {
            values: environmentValues,
        };
        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',
    };
}