Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect parsing/rounding of BigInt #920

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 43 additions & 43 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions templates/node/package.json.twig
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@
},
"devDependencies": {
"@types/node": "20.11.25",
"@types/json-bigint": "^1.0.4",
"tsup": "7.2.0",
"esbuild-plugin-file-path-extensions": "^2.0.0",
"tslib": "2.6.2",
"typescript": "5.4.2"
},
"dependencies": {
"json-bigint": "^1.0.0",
ItzNotABug marked this conversation as resolved.
Show resolved Hide resolved
"node-fetch-native-with-agent": "1.7.2"
}
}
5 changes: 4 additions & 1 deletion templates/node/src/client.ts.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { fetch, FormData, File } from 'node-fetch-native-with-agent';
import { createAgent } from 'node-fetch-native-with-agent/agent';
import { Models } from './models';
import JsonBigInt from 'json-bigint';

const jsonBigInt = JsonBigInt({ storeAsString: false })

type Payload = {
[key: string]: any;
Expand Down Expand Up @@ -170,7 +173,7 @@ class Client {
} else {
switch (headers['content-type']) {
case 'application/json':
options.body = JSON.stringify(params);
options.body = jsonBigInt.stringify(params);
break;

case 'multipart/form-data':
Expand Down
4 changes: 4 additions & 0 deletions templates/web/package.json.twig
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
"build:types": "tsc --declaration --emitDeclarationOnly --outDir types",
"build:libs": "rollup -c"
},
"dependencies": {
"json-bigint": "^1.0.0"
},
"devDependencies": {
"@rollup/plugin-typescript": "8.3.2",
"@types/json-bigint": "^1.0.4",
"playwright": "1.15.0",
"rollup": "2.75.4",
"serve-handler": "6.1.0",
Expand Down
14 changes: 9 additions & 5 deletions templates/web/src/client.ts.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Models } from './models';
import { Service } from './service';
import JsonBigInt from 'json-bigint';

const jsonBigInt = JsonBigInt({ storeAsString: false })

/**
* Payload type representing a key-value pair with string keys and any values.
Expand Down Expand Up @@ -443,7 +446,7 @@ class Client {
},
onMessage: (event) => {
try {
const message: RealtimeResponse = JSON.parse(event.data);
const message: RealtimeResponse = jsonBigInt.parse(event.data);
this.realtime.lastMessage = message;
switch (message.type) {
case 'connected':
Expand All @@ -452,7 +455,7 @@ class Client {
const messageData = <RealtimeResponseConnected>message.data;

if (session && !messageData.user) {
this.realtime.socket?.send(JSON.stringify(<RealtimeRequest>{
this.realtime.socket?.send(jsonBigInt.stringify(<RealtimeRequest>{
type: 'authentication',
data: {
session
Expand Down Expand Up @@ -579,7 +582,7 @@ class Client {
} else {
switch (headers['content-type']) {
case 'application/json':
options.body = JSON.stringify(params);
options.body = jsonBigInt.stringify(params);
break;

case 'multipart/form-data':
Expand All @@ -604,17 +607,18 @@ class Client {
try {
let data = null;
const response = await fetch(url.toString(), options);
const responseData = await response.text()

const warnings = response.headers.get('x-{{ spec.title | lower }}-warning');
if (warnings) {
warnings.split(';').forEach((warning: string) => console.warn('Warning: ' + warning));
}

if (response.headers.get('content-type')?.includes('application/json')) {
data = await response.json();
data = jsonBigInt.parse(responseData)
} else {
data = {
message: await response.text()
message: responseData
};
}

Expand Down
10 changes: 7 additions & 3 deletions templates/web/src/query.ts.twig
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import JsonBigInt from 'json-bigint';

type QueryTypesSingle = string | number | boolean;
export type QueryTypesList = string[] | number[] | boolean[] | Query[];
export type QueryTypes = QueryTypesSingle | QueryTypesList;
type AttributesTypes = string | string[];

const jsonBigInt = JsonBigInt({ storeAsString: false })

/**
* Helper class to generate query strings.
*/
Expand Down Expand Up @@ -41,7 +45,7 @@ export class Query {
* @returns {string}
*/
toString(): string {
return JSON.stringify({
return jsonBigInt.stringify({
method: this.method,
attribute: this.attribute,
values: this.values,
Expand Down Expand Up @@ -248,7 +252,7 @@ export class Query {
* @returns {string}
*/
static or = (queries: string[]) =>
new Query("or", undefined, queries.map((query) => JSON.parse(query))).toString();
new Query("or", undefined, queries.map((query) => jsonBigInt.parse(query))).toString();

/**
* Combine multiple queries using logical AND operator.
Expand All @@ -257,5 +261,5 @@ export class Query {
* @returns {string}
*/
static and = (queries: string[]) =>
new Query("and", undefined, queries.map((query) => JSON.parse(query))).toString();
new Query("and", undefined, queries.map((query) => jsonBigInt.parse(query))).toString();
}
Loading