Skip to content

Commit

Permalink
πŸπŸ”€πŸ« snake_case to camelCase, optimize options validation
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanmcgowan committed Nov 11, 2023
1 parent a025012 commit 20f3fe8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-bags-call.md
@@ -0,0 +1,5 @@
---
"submitjson": patch
---

optimize options key validation + deletion
5 changes: 5 additions & 0 deletions .changeset/tasty-rabbits-learn.md
@@ -0,0 +1,5 @@
---
"submitjson": minor
---

switches from snake_case to camelCase (behind the scenes)
44 changes: 20 additions & 24 deletions index.ts
Expand Up @@ -18,22 +18,22 @@ interface SubmitOptions {
}

type RequestOptions = components['schemas']['SubmissionInput']['options']
type RequestBody = paths['/v1/endpoints/{endpoint_slug}']['post']['requestBody']['content']['application/json']
type RequestBody = paths['/v1/endpoints/{endpointSlug}']['post']['requestBody']['content']['application/json']

export default class SubmitJSON {
private api_key: string
private endpoint_slug: string | undefined
private apiKey: string
private endpointSlug: string | undefined
private options: SubmitOptions | undefined

constructor(config: SubmitJSONConfig) {
this.api_key = config.apiKey
this.endpoint_slug = config.endpoint
this.apiKey = config.apiKey
this.endpointSlug = config.endpoint
this.options = config.options
}

private getHeaders() {
return {
'X-API-Key': this.api_key,
'X-API-Key': this.apiKey,
}
}

Expand All @@ -51,7 +51,7 @@ export default class SubmitJSON {
const s = JSON.parse(data)

if (typeof s !== 'object')
throw new Error(`The string you pass in must parse into an object e.g. { your: 'string' }`)
throw new Error(`πŸ•± The string you pass in must parse into an object e.g. { your: 'string' }`)

d = s
}
Expand All @@ -61,19 +61,19 @@ export default class SubmitJSON {
d = data
}
else {
throw new TypeError('The first argument must be a valid JSON object, string, or FormData')
throw new TypeError('πŸ•± The first argument must be a valid JSON object, string, or FormData')
}
// **HANDLE OPTIONS**
// if second param is a string assume it is an endpoint
if (typeof options === 'string')
endpoint = options

// assign the endpoint slug to the passed in endpoint first, then the config second
const endpoint_slug = endpoint || this.endpoint_slug
const endpointSlug = endpoint || this.endpointSlug

// if no endpoint slug throw error
if (endpoint_slug === undefined)
throw new Error('πŸ‘» No endpoint defined. Add one to your client configuration or to this submit call.')
if (endpointSlug === undefined)
throw new Error('πŸ•± No endpoint defined. Add one to your client configuration or to this submit call.')

// define the body to submit in a sec
const body: RequestBody = { data: d }
Expand All @@ -91,24 +91,20 @@ export default class SubmitJSON {

// check to make sure the options are valid
if (o) {
const requestOptions: RequestOptions = {}
// attach known properties to requestOptions
if (Object.prototype.hasOwnProperty.call(o, 'emailNotification'))
requestOptions.email_notification = o.emailNotification
if (Object.prototype.hasOwnProperty.call(o, 'submissionFormat'))
requestOptions.submission_format = o.submissionFormat
if (Object.prototype.hasOwnProperty.call(o, 'submissionSound'))
requestOptions.submission_sound = o.submissionSound
// ooh yea
if (Object.keys(requestOptions).length > 0)
body.options = requestOptions
// deletes any undefined keys
const { emailNotification, submissionFormat, submissionSound } = o
const options: RequestOptions = { emailNotification, submissionFormat, submissionSound }
Object.keys(options).forEach(key => options && options[key as keyof SubmitOptions] === undefined && delete options[key as keyof SubmitOptions])

if (Object.keys(options).length > 0)
body.options = options
}

// make the submission
const { data: submission, error } = await POST('/v1/endpoints/{endpoint_slug}', {
const { data: submission, error } = await POST('/v1/endpoints/{endpointSlug}', {
headers: this.getHeaders(),
params: {
path: { endpoint_slug },
path: { endpointSlug },
},
body,
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -18,7 +18,7 @@
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"release": "pnpm run build && changeset publish",
"typegen": "openapi-typescript http://localhost:4000/docs.json -o ./v1.d.ts",
"typegen": "openapi-typescript http://localhost:4000/v1/docs.json -o ./v1.d.ts",
"test:ts": "tsc --noEmit"
},
"dependencies": {
Expand Down
34 changes: 18 additions & 16 deletions v1.d.ts
Expand Up @@ -4,7 +4,7 @@
*/

export interface paths {
'/v1/endpoints/{endpoint_slug}': {
'/v1/endpoints/{endpointSlug}': {
parameters: {
query?: never
header?: never
Expand Down Expand Up @@ -44,19 +44,19 @@ export interface components {
* @example raw
* @enum {string}
*/
submission_format?: 'raw' | 'pretty'
submissionFormat?: 'raw' | 'pretty'
/**
* @example ping
* @enum {string}
*/
submission_sound?: 'none' | 'ping'
submissionSound?: 'none' | 'ping'
/** @example false */
email_notification?: boolean
emailNotification?: boolean
}
}
Submission: {
/** Format: date-time */
created_at?: string
createdAt?: string
/**
* @example {
* "name": "Test Testerson",
Expand All @@ -66,31 +66,33 @@ export interface components {
*/
data?: Record<string, never>
/** @example false */
email_notification?: boolean
email_status?: null | string
emailNotification?: boolean
emailStatus?: null | string
/** @example 12345 */
endpoint_id?: number
endpointId?: number
/** @example Test Contact Form */
endpoint_name?: string
endpointName?: string
/** @example XxJqpisK8 */
endpoint_slug?: string
endpointSlug?: string
/** Format: date-time */
seen_at?: string | null
seenAt?: string | null
/** @enum {string} */
submission_format?: 'raw' | 'pretty'
submissionFormat?: 'raw' | 'pretty'
/** @example xxxx-xxxx-xxxx-xxx */
submission_id?: string
submissionId?: string
/**
* @example ping
* @enum {string}
*/
submission_sound?: 'none' | 'ping'
submissionSound?: 'none' | 'ping'
/** @example 12345 */
user_id?: number
userId?: number
}
ErrorResponse: {
/** @example πŸ™ˆ */
message?: string
/** @example 400 */
statusCode?: unknown
}
}
responses: never
Expand All @@ -107,7 +109,7 @@ export interface operations {
header?: never
path: {
/** @description the slug of the endpoint */
endpoint_slug: string
endpointSlug: string
}
cookie?: never
}
Expand Down

0 comments on commit 20f3fe8

Please sign in to comment.