kurier icon indicating copy to clipboard operation
kurier copied to clipboard

Resource Links support

Open spersico opened this issue 5 years ago • 10 comments

We decided to launch to V1 without this feature, but the plan is to someday add support for resource links, and related resource links.

On a first glance, we have 4 big features to implement:

  • Provide Links of the the main resource (ref)
  • Provide Links of the related resources (ref)
  • Provide GET access to the related resources on those links (ref)
  • Provide UPDATE access to the related resources on those links (ref)

spersico avatar Oct 25 '20 02:10 spersico

Any updates on this one? And what about pagination, should this be added to?

  • first: the first page of data
  • last: the last page of data
  • prev: the previous page of data
  • next: the next page of data

https://jsonapi.org/format/#fetching-pagination

isBatak avatar May 08 '21 12:05 isBatak

@spersico would it be possible to add pagination with a custom Addon?

isBatak avatar May 10 '21 12:05 isBatak

hi @isBatak!, it should... but the one that built the addons system is @joelalejandro.

@joelalejandro what do you think?

spersico avatar May 11 '21 14:05 spersico

@isBatak @spersico Hey there! I think it's entirely possible, I made a couple of addons for features that can still be regarded as experimental for Kurier, so sure, it's possible. Once the feature is stable enough, we can consider bundling it as part of the core experience.

joelalejandro avatar May 11 '21 21:05 joelalejandro

@joelalejandro 👋 Can you point out what should I do? For the first step, I just want to support first, last, prev, next links for resources response. I guess I need to extend this part https://github.com/kurierjs/kurier/blob/master/src/application.ts#L228 with links object, but how should I do it through Addon?

isBatak avatar May 12 '21 09:05 isBatak

I figure out how to override buildOperationResponse inside of an addon.

import {
	Addon,
	Application,
	ApplicationInstance,
	HasId,
	KnexProcessor,
	Operation,
	OperationResponse,
	Resource,
} from 'kurier';
import { PaginationOptions } from './types';

const defaults: PaginationOptions = {
	defaultPaginator: 'paged',
	defaultPageSize: 10,
	maximumPageSize: 20,
};

export class PaginationAddon extends Addon {
	constructor(public readonly app: Application, public readonly options?: PaginationOptions) {
		super(app);
		this.options = { ...defaults, ...options };
	}

	async install(): Promise<void> {
		const oldBuildOperationResponse = this.app.buildOperationResponse.bind(this.app);

		this.app.buildOperationResponse = async (
			data: Resource | Resource[] | void,
			appInstance: ApplicationInstance
		): Promise<OperationResponse> => {
			const operationResponse = await oldBuildOperationResponse(data, appInstance);

			let links = {};

			if (Array.isArray(data)) {
			        // TODO
				links = {
					first: '',
					last: '',
					prev: '',
					next: '',
				};
			}

			return {
				...operationResponse,
				links,
			};
		};
	}
}

but the problem is that I need to change this helper also https://github.com/kurierjs/kurier/blob/3ad3b655f8c10b08300e430fd02384486984a933/src/utils/http-utils.ts#L97 ... sad :(

isBatak avatar May 12 '21 22:05 isBatak

@joelalejandro @spersico I made a draft PR #275 to fix the above issue. Could you check if I'm going in the right direction?

isBatak avatar May 13 '21 12:05 isBatak

Sorry for the delay @isBatak, I was on vacation. I will take a look at it today or tomorrow.

joelalejandro avatar May 18 '21 21:05 joelalejandro

@joelalejandro yeah no problem!

isBatak avatar May 19 '21 20:05 isBatak

@isBatak I'm OK with the approach you describe in #275, feel free to go along with it 🚀

joelalejandro avatar May 21 '21 08:05 joelalejandro