{"size": 2432, "ext": "js", "max_stars_repo_path": "src/connectors/TransformConnector.js", "max_stars_repo_name": "seeekr/crudl", "max_stars_repo_stars_event_min_datetime": "2021-05-25T03:43:34.000Z", "max_stars_repo_stars_event_max_datetime": "2021-10-30T01:02:19.000Z", "max_issues_repo_path": "src/connectors/TransformConnector.js", "max_issues_repo_name": "seeekr/crudl", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/connectors/TransformConnector.js", "max_forks_repo_name": "seeekr/crudl", "max_forks_repo_forks_event_min_datetime": "2020-12-31T16:47:12.000Z", "max_forks_repo_forks_event_max_datetime": "2020-12-31T16:47:12.000Z"}
{"max_stars_count": 3, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 29.3012048193, "max_line_length": 74, "alphanum_fraction": 0.6467927632}

const passFunc = x => x

export const defaultTransforms = {
    // Data transforms
    readRequestData: passFunc,
    readResponseData: passFunc,
    updateRequestData: passFunc,
    updateResponseData: passFunc,
    createRequestData: passFunc,
    createResponseData: passFunc,
    deleteRequestData: passFunc,
    deleteResponseData: passFunc,
    // Request and Response transforms
    createRequest: passFunc,
    createResponse: passFunc,
    readRequest: passFunc,
    readResponse: passFunc,
    updateRequest: passFunc,
    updateResponse: passFunc,
    deleteRequest: passFunc,
    deleteResponse: passFunc,
}

class TransformConnector {

    constructor(connector, transforms = {}) {
        this.id = connector.id
        this.connector = connector
        this.transforms = Object.assign({}, defaultTransforms, transforms)
    }

    create(request) {
        const req = this.transforms.createRequest(request)
        req.data = this.transforms.createRequestData(req.data)

        return this.connector.create(req)
        .then((response) => {
            const res = this.transforms.createResponse(response)
            res.data = this.transforms.createResponseData(res.data)
            return res
        })
    }

    read(request) {
        const req = this.transforms.readRequest(request)
        req.data = this.transforms.readRequestData(req.data)

        return this.connector.read(req)
        .then((response) => {
            const res = this.transforms.readResponse(response)
            res.data = this.transforms.readResponseData(res.data)
            return res
        })
    }

    update(request) {
        const req = this.transforms.updateRequest(request)
        req.data = this.transforms.updateRequestData(req.data)

        return this.connector.update(req)
        .then((response) => {
            const res = this.transforms.updateResponse(response)
            res.data = this.transforms.updateResponseData(res.data)
            return res
        })
    }

    delete(request) {
        const req = this.transforms.deleteRequest(request)
        req.data = this.transforms.deleteRequestData(req.data)

        return this.connector.delete(req)
        .then((response) => {
            const res = this.transforms.deleteResponse(response)
            res.data = this.transforms.deleteResponseData(res.data)
            return res
        })
    }
}

export default TransformConnector
