Skip to content

Commit

Permalink
fix: export types, better docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jrea committed Mar 29, 2022
1 parent 2397f3b commit 161ff49
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 30 deletions.
3 changes: 2 additions & 1 deletion lib/nile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
},
"scripts": {
"start": "tsdx watch",
"build": "tsdx build",
"build": "tsdx build && yarn build:types",
"build:types": "tsc -d --declarationDir dist --emitDeclarationOnly",
"test": "tsdx test",
"prepare": "tsdx build",
"size": "size-limit",
Expand Down
82 changes: 56 additions & 26 deletions lib/nile/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
AuthResponse,
UpdatableEntities,
APIResponse,
} from './NileConfig';
} from './types';
import { Requester } from './requester';

const convertToJSON = (res: Response) => {
Expand All @@ -19,25 +19,41 @@ const convertToJSON = (res: Response) => {
// error of some kind
return res;
};

class Nile {
/**
* @type {string} URI to use for requests
* URL for the client to request against.
* @remarks
* The value should be passed in the constructor
*
* @type {string} a FQDN for https requests
* @defaultValue '/'
* @readonly
*/
apiUrl: string;

/**
* auth token saved after login and re-used for requests aginst the backend
* @type {string} token for authorization
* @defaultValue null
* @readonly
*/
authToken: string | null;

/**
* A class wrapping `fetch` for making requests
* @type {Requester} class to make requests
*/
requester: Requester;

/**
* Creates a new instance of Nile
* @param [config] @type {NileConfig}
* Creates a new instance of nile
*
* @remarks
* The main nile client for API integration. One instance should be present for a given application.
*
* @param config - configuration for the nile client
*/
constructor(config?: NileConfig) {
this.apiUrl = config?.apiUrl ?? '/';
Expand All @@ -46,7 +62,8 @@ class Nile {
}

/**
* TODO this will go away when we have cookies
* this will go away when we have cookies
* possibly necessary for node
*/
setRequesterAuth() {
if (!this.requester.authToken) {
Expand All @@ -55,9 +72,9 @@ class Nile {
}

/**
*
* @param payload @type {NileSignIn} sign in payload for a user
* @returns {Promise<boolean>} auth token for use later
* A function to be called for signing in
* @param payload the email and password for a user
* @returns {Promise<boolean>} was login successful
*/
signIn(payload: NileSignIn): Promise<boolean> {
return this.create('login', payload).then(res => {
Expand All @@ -71,48 +88,61 @@ class Nile {
}

/**
* Sends a POST to the be with a payload
*
* @param entity @type {CreateableEntities} maps to urls in the api
* @param payload @type {unknown} the maps to the payload types TODO
* @returns {Promise<EntityType>} the created entity
* @param entity maps to urls in the api
* @param payload the maps to the payload types TODO
* @returns a promise for the created entity
*/
create(entity: CreateableEntities, payload: unknown): Promise<APIResponse> {
return this.requester.fetch('POST', entity, payload).then(convertToJSON);
}

/**
*
* @param entity @type {CreateableEntities | UpdatableEntities} maps to the urls in the api
* @returns {Promise<EntityType>} the created entity
* sends a GET request
* @param entity strings mapped to the urls in the api
* @returns {Promise<APIResponse>} the created entity
*/
read(entity: CreateableEntities | UpdatableEntities): Promise<APIResponse> {
this.setRequesterAuth();
return this.requester.fetch('GET', entity).then(convertToJSON);
}

/**
*
* @param entity @type {UpdatableEntities} maps to the urls in the api
* @param payload @type {unknown} the maps to the payload types TODO
* @returns {Promise<EntityType>} the updated entity
* sends a POST request, with a payload.
* @param entity strings mapped to the urls in the api
* @param payload the id to update, or the maps to the payload types TODO
* @returns {Promise<APIResponse>} the updated entity
*/
update(entity: UpdatableEntities, payload: unknown): Promise<APIResponse> {
update(
entity: UpdatableEntities,
payload: string | { [key: string]: unknown }
): Promise<APIResponse> {
this.setRequesterAuth();
const { id } = payload as { id: string };
let id = payload;
if (typeof id !== 'string') {
id = payload as { id: string };
}
return this.requester
.fetch('POST', `${entity}/${String(id)}`, payload)
.then(convertToJSON);
}

/**
*
* @param entity @type {UpdatableEntities} maps to the urls in the api
* @param payload @type {unknown} the maps to the payload types TODO
* @returns {Promise<EntityType>} the deleted entity
* sends a DELETE request
* @param entity maps to the urls in the api
* @param payload the maps to the payload types TODO
* @returns {Promise<APIResponse>} the deleted entity
*/
delete(entity: UpdatableEntities, payload: unknown): Promise<APIResponse> {
delete(
entity: UpdatableEntities,
payload: string | { [key: string]: unknown }
): Promise<APIResponse> {
this.setRequesterAuth();
const { id } = payload as { id: string };
let id = payload;
if (typeof id !== 'string') {
id = payload as { id: string };
}
return this.requester
.fetch('DELETE', `${entity}/${String(id)}`)
.then(convertToJSON);
Expand Down
2 changes: 1 addition & 1 deletion lib/nile/src/requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
CreateableEntities,
UpdatableEntityUrls,
UpdatableEntities,
} from './NileConfig';
} from './types';

const requestHeaders = (authToken: string | null) => {
const headers: HeadersInit = {
Expand Down
3 changes: 3 additions & 0 deletions lib/nile/src/NileConfig.ts → lib/nile/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/** Options for {@link Nile.constructor} */
export interface NileConfig {
apiUrl: string;
}

/** Payload for {@link Nile.signIn} */
export interface NileSignIn {
email: string;
password: string;
Expand Down
2 changes: 1 addition & 1 deletion lib/nile/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
// error out if import and file system have a casing mismatch. Recommended by TS
"forceConsistentCasingInFileNames": true,
// `tsdx build` ignores this option, but it is commonly used when type-checking separately with `tsc`
"noEmit": true
"noEmit": false
}
}
2 changes: 1 addition & 1 deletion packages/examples/pages/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Edit = () => {
const router = useRouter();
const textRef = useRef('');
const [error, setError] = useState<null | string>(null);
const [users, setUsers] = useState();
const [users, setUsers] = useState<unknown>();
const handleSubmit = useCallback(async () => {
setError(null);
try {
Expand Down

0 comments on commit 161ff49

Please sign in to comment.