Skip to content

Commit

Permalink
feat(type): add types
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm committed Dec 28, 2022
1 parent 5f8f57a commit 2de3842
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 53 deletions.
54 changes: 2 additions & 52 deletions core/fetch/src/type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type Methods = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'TRACE' | 'OPTIONS' | 'PATCH';
import type {Methods, QueryParameters} from '@alwatr/type/src';

export type CacheStrategy = 'network_only' | 'network_first' | 'cache_only' | 'cache_first' | 'stale_while_revalidate';
export type CacheDuplicate = 'never' | 'always' | 'until_load' | 'auto';
export type QueryParameters = Record<string, string | number | boolean>;

export interface FetchOptions extends RequestInit {
/**
Expand Down Expand Up @@ -90,53 +90,3 @@ export interface FetchOptions extends RequestInit {
*/
token?: string;
}

export type AlwatrDocumentObject = {
id: string;
meta?: {
rev: number;
created: number;
updated: number;
};
};

export type AlwatrServiceResponseFailed = {
ok: false;
statusCode: number;
errorCode: string;
meta?: Record<string, unknown>;
data?: never;
};

export type AlwatrServiceResponseSuccess<TData = Record<string, unknown>> = {
ok: true;
statusCode?: number;
errorCode?: never;
meta?: never;
data: TData;
};

export type AlwatrServiceResponseSuccessWithMeta<TData = Record<string, unknown>, TMeta = Record<string, unknown>> = {
ok: true;
statusCode?: number;
errorCode?: never;
meta: TMeta;
data: TData;
};

export type AlwatrServiceResponse<TData = Record<string, unknown>, TMeta = Record<string, unknown>> =
| AlwatrServiceResponseSuccess<TData>
| AlwatrServiceResponseSuccessWithMeta<TData, TMeta>
| AlwatrServiceResponseFailed;

export type AlwatrDocumentMeta = {
formatVersion: number;
reversion: number;
lastUpdated: number;
lastAutoId: number;
};

export type AlwatrDocumentStorage<T extends AlwatrDocumentObject> = Omit<
AlwatrServiceResponseSuccessWithMeta<Record<string, T>, AlwatrDocumentMeta>,
'statusCode' | 'errorCode'
>;
2 changes: 1 addition & 1 deletion core/type/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"name": "@alwatr/type",
"version": "0.26.0",
"description": "Alwatr Types",
"private": true,
"keywords": [
"type",
"typescript",
"esm",
"alwatr"
],
"type": "module",
"main": "type.js",
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com> (https://ali.mihandoost.com)",
"license": "MIT",
"files": [
Expand Down
34 changes: 34 additions & 0 deletions core/type/src/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export type Methods = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'TRACE' | 'OPTIONS' | 'PATCH';

export type QueryParameters = Record<string, string | number | boolean>;
export type ParamKeyType = 'string' | 'number' | 'boolean';
export type ParamValueType = string | number | boolean | null;

export type AlwatrServiceResponseFailed = {
ok: false;
statusCode: number;
errorCode: string;
meta?: Record<string, unknown>;
data?: never;
};

export type AlwatrServiceResponseSuccess<TData = Record<string, unknown>> = {
ok: true;
statusCode?: number;
errorCode?: never;
meta?: never;
data: TData;
};

export type AlwatrServiceResponseSuccessWithMeta<TData = Record<string, unknown>, TMeta = Record<string, unknown>> = {
ok: true;
statusCode?: number;
errorCode?: never;
meta: TMeta;
data: TData;
};

export type AlwatrServiceResponse<TData = Record<string, unknown>, TMeta = Record<string, unknown>> =
| AlwatrServiceResponseSuccess<TData>
| AlwatrServiceResponseSuccessWithMeta<TData, TMeta>
| AlwatrServiceResponseFailed;
23 changes: 23 additions & 0 deletions core/type/src/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type {AlwatrServiceResponseSuccessWithMeta} from './service.js';

export type AlwatrDocumentObject = {
id: string;
meta?: {
rev: number;
created: number;
updated: number;
};
};

export type AlwatrDocumentMeta = {
formatVersion: number;
reversion: number;
lastUpdated: number;
lastAutoId: number;
};


export type AlwatrDocumentStorage<T extends AlwatrDocumentObject> = Omit<
AlwatrServiceResponseSuccessWithMeta<Record<string, T>, AlwatrDocumentMeta>,
'statusCode' | 'errorCode'
>;
5 changes: 5 additions & 0 deletions core/type/src/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './storage.js';
export * from './service.js';

// FIXME:
export type MaybePromise<T> = T | Promise<T>

0 comments on commit 2de3842

Please sign in to comment.