shx/packages/api/services/urlstore.service.ts

42 lines
1.0 KiB
TypeScript

import { gql } from 'graphql-request';
import { client } from '../helpers';
import { UserMeta } from '../types';
import { IURLStoreService } from '../interfaces/urlstore.interface';
export default class URLStore implements IURLStoreService {
public async storeURLS(url: string, meta: UserMeta) {
const query = gql`
mutation insertURL($object: shorturls_insert_input!) {
insert_shorturls_one(object: $object) {
short_key
original_url
}
}
`;
const variables = {
object: {
original_url: url,
creator_ip: meta.ip,
apikeyUsed: meta.apiKeyID,
},
};
const result: any = await client.request(query, variables);
return result.insert_shorturls_one;
}
public async getURLS(short_key: string) {
const query = gql`
query getURL($short_key: String!) {
shorturls(where: { short_key: { _eq: $short_key } }) {
original_url
}
}
`;
const variables = {
short_key,
};
const result: any = await client.request(query, variables);
return result.shorturls[0];
}
}