import { isClient as isClient$1, createClient as createClient$1, Client, hasOwnProperty, verifyInstanceBySymbol } from 'hapic';
export { isClientError, isClientErrorDueNetworkIssue, isClientErrorWithStatusCode } from 'hapic';

/*
 * Copyright (c) 2023.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */ var HeaderName;
(function(HeaderName) {
    HeaderName["CONTENT_TYPE"] = 'Content-Type';
    HeaderName["VAULT_NAMESPACE"] = 'X-Vault-Namespace';
    HeaderName["VAULT_REQUEST"] = 'X-Vault-Request';
})(HeaderName || (HeaderName = {}));

/*
 * Copyright (c) 2022.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */ var MountType;
(function(MountType) {
    MountType["SSH"] = 'ssh';
    MountType["KEY_VALUE"] = 'kv';
})(MountType || (MountType = {}));
var KeyValueVersion;
(function(KeyValueVersion) {
    KeyValueVersion[KeyValueVersion["ONE"] = 1] = "ONE";
    KeyValueVersion[KeyValueVersion["TWO"] = 2] = "TWO";
})(KeyValueVersion || (KeyValueVersion = {}));

class BaseAPI {
    // -----------------------------------------------------------------------------------
    setDriver(input) {
        if (isClient$1(input)) {
            this.client = input;
        } else {
            this.client = createClient$1(input);
        }
    }
    // -----------------------------------------------------------------------------------
    constructor(context){
        context = context || {};
        this.setDriver(context.client);
    }
}

class MountAPI extends BaseAPI {
    async create(path, data) {
        data.options = data.options || {};
        data.config = data.config || {};
        const response = await this.client.post(`sys/mounts/${path}`, data);
        return response.data;
    }
    async update(path, payload) {
        const response = await this.client.post(`sys/mounts/${path}`, payload);
        return response.data;
    }
    async getMany() {
        const response = await this.client.get('sys/mounts');
        return response.data;
    }
    async getOne(path) {
        const response = await this.client.get(`sys/mounts/${path}`);
        return response.data;
    }
    async delete(path) {
        await this.client.delete(`sys/mounts/${path}`);
    }
    // eslint-disable-next-line no-useless-constructor,@typescript-eslint/no-useless-constructor
    constructor(context){
        super(context);
    }
}

class KeyValueV1API extends BaseAPI {
    /**
     * Create a secret.
     *
     * @param mount The mount point on which the secret engine is mounted.
     * @param path The secret path relative to the secret engine mount.
     * @param data
     */ async create(mount, path, data) {
        const response = await this.client.post(`${mount}/${path}`, data);
        return response.data;
    }
    /**
     * Get a secret.
     *
     * @param mount The mount point on which the secret engine is mounted.
     * @param path The secret path relative to the secret engine mount.
     */ async getOne(mount, path) {
        const { data } = await this.client.get(`${mount}/${path}`);
        return data;
    }
    /**
     * Delete a secret.
     *
     * @param mount The mount point on which the secret engine is mounted.
     * @param path The secret path relative to the secret engine mount.
     */ async delete(mount, path) {
        await this.client.delete(`${mount}/${path}`);
    }
    // eslint-disable-next-line no-useless-constructor,@typescript-eslint/no-useless-constructor
    constructor(context){
        super(context);
    }
}

class KeyValueV2API extends BaseAPI {
    /**
     * Create a secret.
     *
     * @param mount The mount point on which the secret engine is mounted.
     * @param path The secret path relative to the secret engine mount.
     * @param data
     */ async create(mount, path, data) {
        const response = await this.client.post(`${mount}/data/${path}`, data);
        return response.data;
    }
    /**
     * Get a secret (version).
     *
     * @param mount The mount point on which the secret engine is mounted.
     * @param path The secret path relative to the secret engine mount.
     * @param version
     */ async getOne(mount, path, version) {
        const { data } = await this.client.get(`${mount}/data/${path}?version=${version || 0}`);
        return data;
    }
    /**
     * Update a secret.
     *
     * @param mount The mount point on which the secret engine is mounted.
     * @param path The secret path relative to the secret engine mount.
     * @param data
     */ async update(mount, path, data) {
        const response = await this.client.patch(`${mount}/data/${path}`, data);
        return response.data;
    }
    /**
     * Delete a secret.
     *
     * @param mount The mount point on which the secret engine is mounted.
     * @param path The secret path relative to the secret engine mount.
     */ async delete(mount, path) {
        await this.client.delete(`${mount}/metadata/${path}`);
    }
    /**
     * Create or update a secret.
     *
     * @param mount The mount point on which the secret engine is mounted.
     * @param path The secret path relative to the secret engine mount.
     * @param data
     */ async save(mount, path, data) {
        return this.create(mount, path, data);
    }
    // eslint-disable-next-line no-useless-constructor,@typescript-eslint/no-useless-constructor
    constructor(context){
        super(context);
    }
}

/*
 * Copyright (c) 2021-2021.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */ class ConnectionStringParseError extends Error {
    static connectionStringMissing(serviceName) {
        const parts = [
            'The'
        ];
        if (typeof serviceName === 'string') {
            parts.push(serviceName);
        }
        parts.push('connection string is not specified.');
        return new this(parts.join(' '));
    }
    static connectionStringInvalid(serviceName) {
        const parts = [
            'The'
        ];
        if (typeof serviceName === 'string') {
            parts.push(serviceName);
        }
        parts.push('connection string is not valid.');
        return new this(parts.join(' '));
    }
}

function parseConnectionString(connectionString) {
    const parts = connectionString.split('@');
    if (parts.length !== 2) {
        throw new ConnectionStringParseError('Vault connection string must be in the following format: token@host');
    }
    return {
        host: parts[1],
        token: parts[0]
    };
}

class VaultClient extends Client {
    // -----------------------------------------------------------------------------------
    applyConfig(input) {
        input = input || {};
        this.setHeader(HeaderName.VAULT_REQUEST, 'true');
        this.setHeader(HeaderName.CONTENT_TYPE, 'application/json');
        let connectionOptions;
        if (input.connectionString) {
            connectionOptions = parseConnectionString(input.connectionString);
        }
        if (input.connectionOptions) {
            connectionOptions = input.connectionOptions;
        }
        if (connectionOptions) {
            this.setBaseURL(connectionOptions.host);
            this.setHeader('X-Vault-Token', connectionOptions.token);
        }
        return this;
    }
    // -----------------------------------------------------------------------------------
    setNamespace(namespace) {
        this.setHeader(HeaderName.VAULT_NAMESPACE, namespace);
    }
    unsetNamespace() {
        this.unsetHeader(HeaderName.VAULT_NAMESPACE);
    }
    // -----------------------------------------------------------------------------------
    constructor(input){
        input = input || {};
        super(input.request);
        this['@instanceof'] = Symbol.for('VaultClient');
        this.mount = new MountAPI({
            client: this
        });
        this.keyValueV1 = new KeyValueV1API({
            client: this
        });
        this.keyValueV2 = new KeyValueV2API({
            client: this
        });
        this.applyConfig(input);
    }
}

const instances = {};
/**
 * Verify if a vault client singleton instance exists.
 *
 * @param key
 */ function hasClient(key) {
    return hasOwnProperty(instances, key || 'default');
}
/**
 * Set the vault client singleton instance.
 *
 * @param client
 * @param key
 */ function setClient(client, key) {
    key = key || 'default';
    instances[key] = client;
    return client;
}
/**
 * Receive a vault singleton instance.
 *
 * @param key
 */ function useClient(key) {
    key = key || 'default';
    if (Object.prototype.hasOwnProperty.call(instances, key)) {
        return instances[key];
    }
    const instance = createClient();
    instances[key] = instance;
    return instance;
}
/**
 * Unset a vault client singleton instance.
 *
 * @param key
 */ function unsetClient(key) {
    key = key || 'default';
    if (hasOwnProperty(instances, key)) {
        delete instances[key];
    }
}
/**
 * Create a vault client.
 *
 * @param input
 */ function createClient(input) {
    return new VaultClient(input);
}
/**
 * Check if the argument is of instance Client.
 *
 * @param input
 */ function isClient(input) {
    if (input instanceof VaultClient) {
        return true;
    }
    return verifyInstanceBySymbol(input, 'VaultClient');
}

const client = createClient();

export { ConnectionStringParseError, HeaderName, KeyValueV1API, KeyValueV2API, KeyValueVersion, MountAPI, MountType, VaultClient, createClient, client as default, hasClient, isClient, parseConnectionString, setClient, unsetClient, useClient };
//# sourceMappingURL=index.mjs.map
