feat: camelizer util

This commit is contained in:
Skillz
2022-12-31 13:10:59 -06:00
parent 2b868edf1f
commit bc7a69bcd1
4 changed files with 34 additions and 8 deletions

View File

@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/restrict-template-expressions */
import TRANSFORMERS from '@discordeno/transformer'
import type { BigString, Camelize, CreateMessageOptions, DiscordCreateMessage, DiscordMessage, DiscordUser, GetMessagesOptions } from '@discordeno/types'
import { delay } from '@discordeno/utils'
import { delay, camelize } from '@discordeno/utils'
// TODO: make dynamic based on package.json file
const version = '18.0.0-alpha.1'
@@ -162,16 +162,15 @@ export function createRestManager (options: CreateRestManagerOptions): RestManag
},
async get (url) {
return await rest.makeRequest('GET', url)
return camelize(await rest.makeRequest('GET', url))
},
async post (url, body) {
return await rest.makeRequest('POST', url, body)
return camelize(await rest.makeRequest('POST', url, body))
},
async getUser (id) {
const result = await rest.get<DiscordUser>(rest.routes.user(id))
return TRANSFORMERS.user(result)
return await rest.get<DiscordUser>(rest.routes.user(id))
},
/**
@@ -262,9 +261,9 @@ export interface RestManager {
/** Make a request to be sent to the api. */
makeRequest: <T = unknown>(method: RequestMethods, url: string, body?: Record<string, any>) => Promise<T>
/** Make a get request to the api */
get: <T = unknown>(url: string) => Promise<T>
get: <T = unknown>(url: string) => Promise<Camelize<T>>
/** Make a post request to the api. */
post: <T = unknown>(url: string, body?: Record<string, any>) => Promise<T>
post: <T = unknown>(url: string, body?: Record<string, any>) => Promise<Camelize<T>>
/**
* Get a user's data from the api
*

View File

@@ -1,4 +1,4 @@
import type { BigString } from "./shared"
import type { BigString } from './shared'
export interface CreateMessageOptions {
/** The message contents (up to 2000 characters) */

View File

@@ -0,0 +1,26 @@
import type { Camelize } from '@discordeno/types'
export const camelize = <T>(object: T): Camelize<T> => {
if (Array.isArray(object)) {
return object.map((element) =>
camelize(element)
) as Camelize<T>
}
if (typeof object === 'object' && object !== null) {
const obj = {} as Camelize<T>;
(Object.keys(object) as Array<keyof T>).forEach((key) => {
// @ts-expect-error
(obj[
typeof key === 'string'
? key.replace(/([-_][a-z])/gi, ($1) => {
return $1.toUpperCase().replace('-', '').replace('_', '')
})
: key
] as Camelize<(T & object)[keyof T]>) = camelize(
object[key]
)
})
return obj
}
return object as Camelize<T>
}

View File

@@ -1 +1,2 @@
export * from './casing.js'
export * from './utils.js'