-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(recipes/front_end): add initial api files
- Loading branch information
ttapia
committed
Sep 2, 2022
1 parent
4fbb9f6
commit aaf9599
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import axios, { type AxiosRequestTransformer, type AxiosResponseTransformer } from 'axios'; | ||
import convertKeys from '../utils/case-converter'; | ||
|
||
const api = axios.create({ | ||
transformRequest: [ | ||
(data: any) => convertKeys(data, 'decamelize'), | ||
...(axios.defaults.transformRequest as AxiosRequestTransformer[]), | ||
], | ||
transformResponse: [ | ||
...(axios.defaults.transformResponse as AxiosResponseTransformer[]), | ||
(data: any) => convertKeys(data, 'camelize'), | ||
], | ||
}); | ||
|
||
/* | ||
// Example to use the api object in the path ´app/javascript/api/users.ts´ | ||
import api from './index'; | ||
export default { | ||
index() { | ||
const path = '/api/internal/users'; | ||
return api({ | ||
method: 'get', | ||
url: path, | ||
}); | ||
}, | ||
create(data: Partial<User>) { | ||
const path = '/api/internal/users'; | ||
return api({ | ||
method: 'post', | ||
url: path, | ||
data: { | ||
user: data, | ||
}, | ||
}); | ||
}, | ||
update(data: Partial<User>) { | ||
const path = `/api/internal/users/${data.id}`; | ||
return api({ | ||
method: 'put', | ||
url: path, | ||
data: { | ||
user: data, | ||
}, | ||
}); | ||
}, | ||
}; | ||
*/ |
39 changes: 39 additions & 0 deletions
39
lib/potassium/assets/app/javascript/utils/case-converter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// From https://github.com/domchristie/humps/issues/51#issuecomment-425113505 | ||
/* eslint-disable complexity */ | ||
/* eslint-disable max-statements */ | ||
import { camelize, decamelize } from 'humps'; | ||
|
||
type objectToConvert = File | FormData | Blob | Record<string, unknown> | Array<objectToConvert>; | ||
|
||
function convertKeys( | ||
object: objectToConvert, | ||
conversion: 'camelize' | 'decamelize', | ||
): objectToConvert { | ||
const converter = { | ||
camelize, | ||
decamelize, | ||
}; | ||
if (object && !(object instanceof File) && !(object instanceof Blob)) { | ||
if (object instanceof Array) { | ||
return object.map((item: objectToConvert) => convertKeys(item, conversion)); | ||
} | ||
if (object instanceof FormData) { | ||
const formData = new FormData(); | ||
for (const [key, value] of object.entries()) { | ||
formData.append(converter[conversion](key), value); | ||
} | ||
|
||
return formData; | ||
} | ||
if (typeof object === 'object') { | ||
return Object.keys(object).reduce((acc, next) => ({ | ||
...acc, | ||
[converter[conversion](next)]: convertKeys(object[next] as objectToConvert, conversion), | ||
}), {}); | ||
} | ||
} | ||
|
||
return object; | ||
} | ||
|
||
export default convertKeys; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters