Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@trapi/query

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@trapi/query - npm Package Compare versions

Comparing version 2.1.3 to 2.1.5

6

package.json
{
"name": "@trapi/query",
"version": "2.1.3",
"version": "2.1.5",
"description": "A tiny library which provides utility types/functions for request and response query handling.",

@@ -45,3 +45,3 @@ "main": "./dist/index.js",

"dependencies": {
"minimatch": "^5.0.1"
"minimatch": "^5.1.0"
},

@@ -51,3 +51,3 @@ "publishConfig": {

},
"gitHead": "12fc7cc4be787779fee81bedb9a4cff0e2f5055d"
"gitHead": "2db0dd50542e1984c62c7aa5c0712c7380beb1a5"
}

@@ -8,4 +8,11 @@ # @trapi/query 🌈

This is a library to build efficient and optimized `JSON:API` like REST-APIs.
---
**Important NOTE**
This package has been replaced by another package, called: [rapiq](https://www.npmjs.com/package/rapiq).
---
This is a library to build efficient and `JSON:API` like REST-APIs.
It extends the specification format between request- & response-handling based on the following parameters:

@@ -28,9 +35,2 @@ - `fields`

---
**Important NOTE**
The examples in the [Parsing](#parsing-) section, are not available with releases `<1.0.0` of the `typeorm-extension` library.
---
**Table of Contents**

@@ -71,15 +71,11 @@

The general idea is to construct a [BuildInput](#buildinput) at the frontend side,
which will be formatted to a `string` and passed to the backend application as a URL query string.
As a result the backend application will always able to process the request.
The general idea is to construct a [BuildInput](#buildinput) object and convert it to a string
representation with the [buildQuery](#buildquery) method on the frontend side.
The result should then be provided as a URL query string to the backend application.
The backend application is then able to process the request, by parsing and interpreting the query string.
Therefore, two components of this module are required in the frontend application:
- generic type: `BuildInput<T>`
- function: `buildQuery`.
The following example should give an insight on how to use this library on the frontend side.
Therefore, a type which will represent a `User` and a method `getAPIUsers` are defined.
The method should perform a request to the resource API to receive a collection of entities.
The method will generate the query string.
In the following example a Class which will represent the structure of a `User` and a function called
`getAPIUsers`, which will handle the resource request to the resource API, will be defined.
```typescript

@@ -92,3 +88,3 @@ import axios from "axios";

class Profile {
type Profile = {
id: number;

@@ -99,3 +95,3 @@ avatar: string;

class User {
type User = {
id: number;

@@ -154,8 +150,8 @@ name: string;

The next examples will demonstrate how to parse and validate the transformed `BuildInput<T>` on the backend side.
The next section will describe, on how to parse and interpret the query string on the backend side.
### Parsing 🔎
For explanation purposes,
two simple entities with a basic relation between them are declared to demonstrate their usage:
For explanation purposes, two simple entities with a basic relation between them are declared to demonstrate the usage on the backend side:
**`entities.ts`**
```typescript

@@ -202,12 +198,9 @@ import {

```
In this example [typeorm](https://www.npmjs.com/package/typeorm) is used for `object-relational mapping (ORM)` and [typeorm-extension](https://www.npmjs.com/package/typeorm-extension) is used
to apply the parsed request query parameters on the db query and [express](https://www.npmjs.com/package/express) to handle requests.
#### Parse - Extended
#### Parse - Detailed
In the following example, all query parameter parse functions (`parseQueryFields`, `parseQueryFilters`, ...)
will be imported separately.
In the following code snippet, all query parameter parse functions (`parseQueryFields`, `parseQueryFilters`, ...)
will be imported separately, to show the usage in detail.
```typescript
import {getRepository} from "typeorm";
import {Request, Response} from 'express';

@@ -225,5 +218,8 @@

import {
applyQueryParseOutput
} from "typeorm-extension";
applyQueryParseOutput,
useDataSource
} from 'typeorm-extension';
import { User } from './entities';
/**

@@ -252,3 +248,4 @@ * Get many users.

const repository = getRepository(User);
const dataSource = await useDataSource();
const repository = dataSource.getRepository(User);
const query = repository.createQueryBuilder('user');

@@ -318,13 +315,7 @@

#### Parse - Short
Another way is to directly import the `parseQuery` function, which will handle a group of query parameter values & options.
Another way is to directly import the [parseQuery](#parsequery) method, which will handle a group of query parameter values & options.
The [ParseInput](#parseinput) argument of the `parseQuery` method accepts multiple (alias-) property keys.
The [ParseInput](#parseinput) data of the `parseQuery` function can have the following (alias-) property keys, which will be formatted to [Parameter](#parameter)
keys after the parse process:
- `fields` | `Parameter.FIELDS`
- `include` | `relations` | `Parameter.RELATIONS`
- ... [more](#parseinput)
```typescript
import {getRepository} from "typeorm";
import {Request, Response} from 'express';
import { Request, Response } from 'express';

@@ -335,7 +326,8 @@ import {

ParseOutput
} from "@trapi/query";
} from '@trapi/query';
import {
applyQueryParseOutput
} from "typeorm-extension";
applyQueryParseOutput,
useDataSource
} from 'typeorm-extension';

@@ -374,3 +366,4 @@ /**

const repository = getRepository(User);
const dataSource = await useDataSource();
const repository = dataSource.getRepository(User);
const query = repository.createQueryBuilder('user');

@@ -401,4 +394,5 @@

It can even be much shorter to parse the query key values, because `typeorm-extension` uses `@trapi/query` under the hood ⚡.
This is much shorter than the previous example and has less direct dependencies 😁.
It can even be much simpler to parse the query key-value pairs with the `typeorm-extension` library, because it
uses this library under the hood ⚡.
This is much shorter than the previous example and has less explicit dependencies 😁.

@@ -443,5 +437,5 @@ [read more](https://www.npmjs.com/package/typeorm-extension)

| Name | Description |
| :------ | :------ |
| `T` | A type, interface, or class which represent the data structure.
| Name | Description |
|:-------|:----------------------------------------------------------------|
| `T` | A type, interface, or class which represent the data structure. |

@@ -451,6 +445,6 @@

| Name | Type | Description |
| :------ | :------ | :------ |
| `input` | `BuildInput`<`T`> | Input specification [more](#buildinput). |
| `options` | `BuildOptions` | Options for building fields, filter, include, ...
| Name | Type | Description |
|:----------|:------------------|:--------------------------------------------------|
| `input` | `BuildInput`<`T`> | Input specification [more](#buildinput). |
| `options` | `BuildOptions` | Options for building fields, filter, include, ... |

@@ -484,3 +478,2 @@ #### Returns

} from "@trapi/query";
import {URLParameter} from "@trapi/query/src";

@@ -507,4 +500,4 @@ const output: ParseOutput = parseQuery({

| Name | Description |
| :------ | :------ |
| Name | Description |
|:------|:------------|

@@ -514,6 +507,6 @@

| Name | Type | Description |
| :------ | :------ | :------ |
| `input` | `ParseInput` | Query input data passed e.g. via URL [more](#parseinput). |
| `options` | `ParseOptions` | Options for parsing fields, filter, include, ... [more](#parseoptions)
| Name | Type | Description |
|:----------|:---------------|:-----------------------------------------------------------------------|
| `input` | `ParseInput` | Query input data passed e.g. via URL [more](#parseinput). |
| `options` | `ParseOptions` | Options for parsing fields, filter, include, ... [more](#parseoptions) |

@@ -664,4 +657,4 @@ #### Returns

| Name | Description |
| :------ | :------ |
| Name | Description |
|:------|:------------|

@@ -671,6 +664,6 @@

| Name | Type | Description |
| :------ | :------ | :------ |
| `input` | `unknown` | Query input data passed e.g. via URL [more](#parseinput). |
| `options` | `ParseParameterOptions<Parameter>` | Options for parsing fields, filter, include, ... [more](#parseoptions)
| Name | Type | Description |
|:----------|:-----------------------------------|:-----------------------------------------------------------------------|
| `input` | `unknown` | Query input data passed e.g. via URL [more](#parseinput). |
| `options` | `ParseParameterOptions<Parameter>` | Options for parsing fields, filter, include, ... [more](#parseoptions) |

@@ -734,3 +727,3 @@ #### Returns

- `ParseParameterOptions<T extends ParameterType | URLParameterType>`
is a generic type and returns the available options for a given parameter type, i.e:
is a generic type and returns the available options for a given parameter type, e.g.
- [FieldsParseOptions](#fields),

@@ -740,3 +733,3 @@ - [FiltersParseOptions](#filters)

- `ParseParameterOutput<T extends ParameterType | URLParameterType>`
is a generic type and returns the parsed output data for a given parameter type, i.e:
is a generic type and returns the parsed output data for a given parameter type, e.g.
- [FieldsParseOutput](#fields)

@@ -743,0 +736,0 @@ - [FiltersParseOutput](#filters)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc