import superagent from 'superagent';

export class StoreClient {
    constructor(port = 3333) {
        this.baseUrl = `http://localhost:${port}`;
    }

    async setValue(key, value) {
        const response = await superagent
            .post(`${this.baseUrl}/set/${key}`)
            .send({ value })
            .set('Content-Type', 'application/json');
        return response.body;
    }

    async getValue(key) {
        try {
            const response = await superagent
                .get(`${this.baseUrl}/get/${key}`);
            return response.body.value;
        } catch (error) {
            if (error.status === 404) {
                return null;
            }
            throw error;
        }
    }

    async deleteValue(key) {
        const response = await superagent
            .delete(`${this.baseUrl}/delete/${key}`);
        return response.body;
    }

    async clear() {
        const response = await superagent
            .post(`${this.baseUrl}/clear`);
        return response.body;
    }
} 