Skip to content

Commit

Permalink
fix: include type of $id in document generic
Browse files Browse the repository at this point in the history
  • Loading branch information
stropitek committed Feb 14, 2022
1 parent 9403a17 commit 9eb7632
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 37 deletions.
23 changes: 13 additions & 10 deletions src/roc/Roc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,25 @@ export default class Roc {
this.dbRequest = createAxios(this.dbUrl, config.accessToken);
}

public async create<ContentType>(newDocument: INewDocument<ContentType>) {
public async create<ContentType, IdType>(
newDocument: INewDocument<ContentType, IdType>,
) {
const response = await this.dbRequest.post('entry', newDocument);
return this.getDocument(response.data.id);
}

public initializeDocument<ContentType = Record<string, unknown>>(
data: IDocument<ContentType>,
public initializeDocument<ContentType, IdType>(
data: IDocument<ContentType, IdType>,
options?: RocDocumentOptions,
) {
const url = new URL(`entry/${data._id}/`, this.dbUrl).href;
return new RocDocument(data, createAxios(url, this.accessToken), options);
}

public getDocument<ContentType = Record<string, unknown>>(
public getDocument<ContentType, IdType>(
uuid: string,
options?: RocDocumentOptions,
): RocDocument<ContentType> {
): RocDocument<ContentType, IdType> {
const url = new URL(`entry/${uuid}/`, this.dbUrl).href;
return new RocDocument(uuid, createAxios(url, this.accessToken), options);
}
Expand Down Expand Up @@ -123,11 +125,12 @@ export default class Roc {
);
}

public getView<KeyType = unknown, ContentType = unknown>(
viewName: string,
options: IViewOptions<KeyType> = {},
) {
return new View<ContentType>(
public getView<
KeyType = unknown,
ContentType = Record<string, unknown>,
IdType = unknown,
>(viewName: string, options: IViewOptions<KeyType> = {}) {
return new View<ContentType, IdType>(
viewName,
options,
createAxios(
Expand Down
15 changes: 9 additions & 6 deletions src/roc/RocDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ export interface RocDocumentOptions {
const defaultRocOptions: RocDocumentOptions = {
allowAttachmentOverwrite: true,
};
export default class RocDocument<ContentType = Record<string, unknown>> {
export default class RocDocument<
ContentType = Record<string, unknown>,
IdType = string,
> {
private request: AxiosInstance;
public uuid: string;
public rev?: string;
protected value?: IDocument<ContentType>;
protected value?: IDocument<ContentType, IdType>;
public deleted: boolean;
private options: RocDocumentOptions;

public constructor(
data: string | IDocument<ContentType>,
data: string | IDocument<ContentType, IdType>,
request: AxiosInstance,
options: RocDocumentOptions = defaultRocOptions,
) {
Expand Down Expand Up @@ -50,7 +53,7 @@ export default class RocDocument<ContentType = Record<string, unknown>> {
return response.data;
}

public async fetch(rev?: string): Promise<IDocument<ContentType>> {
public async fetch(rev?: string): Promise<IDocument<ContentType, IdType>> {
if (rev) {
throw new Error('UNIMPLEMENTED fetch with rev');
}
Expand All @@ -63,9 +66,9 @@ export default class RocDocument<ContentType = Record<string, unknown>> {
content: ContentType,
newAttachments?: INewAttachment[],
deleteAttachments?: string[],
): Promise<IDocument<ContentType>> {
): Promise<IDocument<ContentType, IdType>> {
await this._fetchIfUnfetched();
let newDoc: IDocumentDraft<ContentType> = {
let newDoc: IDocumentDraft<ContentType, IdType> = {
...this.value,
$content: content,
};
Expand Down
6 changes: 3 additions & 3 deletions src/roc/View.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AxiosInstance } from 'axios';

import { IViewOptions, IViewResult } from '../types';

export default class View<ContentType> {
export default class View<ContentType, IdType> {
private request: AxiosInstance;
private options: IViewOptions;
public readonly viewName: string;
Expand All @@ -18,15 +18,15 @@ export default class View<ContentType> {
}

public then(
resolve: (value: IViewResult<ContentType>) => void,
resolve: (value: IViewResult<ContentType, IdType>) => void,
reject: (error: Error) => void,
) {
this.fetch().then(resolve, reject);
}

public async fetch(
options: IViewOptions = {},
): Promise<IViewResult<ContentType>> {
): Promise<IViewResult<ContentType, IdType>> {
const params = Object.assign({}, this.options, options);

const response = await this.request({
Expand Down
10 changes: 5 additions & 5 deletions src/roc/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
INewAttachment,
} from '../types';

export async function addInlineUploads<ContentType>(
entry: IDocumentDraft<ContentType>,
export async function addInlineUploads<ContentType, IdType>(
entry: IDocumentDraft<ContentType, IdType>,
attachments: INewAttachment[],
) {
const attachmentsBase64 = await Promise.all(
Expand Down Expand Up @@ -39,7 +39,7 @@ export async function addInlineUploads<ContentType>(
}),
);

const newEntry = produce(entry, (draft: IDocumentDraft) => {
const newEntry = produce(entry, (draft: IDocumentDraft<unknown, unknown>) => {
for (let i = 0; i < attachments.length; i++) {
const newAttachment: ICouchInlineAttachment = {
content_type: attachments[i].content_type,
Expand All @@ -55,8 +55,8 @@ export async function addInlineUploads<ContentType>(
return newEntry;
}

export function deleteInlineUploads<ContentType>(
entry: IDocumentDraft<ContentType>,
export function deleteInlineUploads<ContentType, IdType>(
entry: IDocumentDraft<ContentType, IdType>,
attachmentNames: string[],
) {
return produce(entry, (draft) => {
Expand Down
6 changes: 4 additions & 2 deletions src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ export const testRoc = new Roc({
url: 'http://localhost:3000',
});

type TestNewDoc = INewDocument<{ hello: 'world' }, string>;

export function getNewEntry(id: string) {
return {
$id: id,
$kind: 'entry',
$content: { hello: 'world' },
$owners: [],
} as INewDocument;
} as TestNewDoc;
}

export function getNewDocument(kind: string, id: string) {
Expand All @@ -59,5 +61,5 @@ export function getNewDocument(kind: string, id: string) {
$kind: kind,
$content: { hello: 'world' },
$owners: [],
} as INewDocument;
} as TestNewDoc;
}
23 changes: 12 additions & 11 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export interface IRocDocumentOptions {
pollInterval?: number;
}

export interface INewDocument<ContentType = Record<string, unknown>> {
$id: unknown;
export interface INewDocument<ContentType, IdType> {
$id: IdType;
$content: ContentType;
$kind: string;
$owners: string[];
Expand All @@ -30,8 +30,8 @@ export interface INewRevisionMeta {
_rev: string;
}

export interface IBaseDocument<ContentType = Record<string, unknown>>
extends INewDocument<ContentType>,
export interface IBaseDocument<ContentType, IdType>
extends INewDocument<ContentType, IdType>,
INewRevisionMeta {
_id: string;
$type: 'entry' | 'group';
Expand All @@ -40,15 +40,15 @@ export interface IBaseDocument<ContentType = Record<string, unknown>>
$lastModification: string;
}

export interface IDocument<ContentType = Record<string, unknown>>
extends IBaseDocument<ContentType> {
export interface IDocument<ContentType, IdType>
extends IBaseDocument<ContentType, IdType> {
_attachments: {
[key: string]: ICouchAttachmentStub;
};
}

export interface IDocumentDraft<ContentType = Record<string, unknown>>
extends IBaseDocument<ContentType> {
export interface IDocumentDraft<ContentType, IdType>
extends IBaseDocument<ContentType, IdType> {
_attachments?: {
[key: string]: ICouchAttachment | ICouchInlineAttachment;
};
Expand Down Expand Up @@ -131,15 +131,16 @@ export interface IQueryResult<
KeyType = unknown,
ValueType = unknown,
ContentType = Record<string, unknown>,
IdType = unknown,
> {
id: string;
key: KeyType;
doc?: IDocument<ContentType>;
doc?: IDocument<ContentType, IdType>;
value: ValueType;
}

export type IViewResult<ContentType = Record<string, unknown>> = Array<
IDocument<ContentType>
export type IViewResult<ContentType, IdType> = Array<
IDocument<ContentType, IdType>
>;

export interface IViewOptions<KeyType = unknown> {
Expand Down

0 comments on commit 9eb7632

Please sign in to comment.