Skip to content

Commit bbd1ebe

Browse files
laljikanjareeyacrwilcox
authored andcommitted
fix!: keyToLegacyUrlsafe is now an async method (#496)
1 parent 6c8cc74 commit bbd1ebe

File tree

2 files changed

+122
-16
lines changed

2 files changed

+122
-16
lines changed

src/index.ts

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {entity} from './entity';
3434
import {Query} from './query';
3535
import {DatastoreRequest} from './request';
3636
import {Transaction} from './transaction';
37+
import {promisifyAll} from '@google-cloud/promisify';
3738

3839
const {grpc} = new GrpcClient();
3940

@@ -730,6 +731,16 @@ class Datastore extends DatastoreRequest {
730731
return Datastore.isKey(value);
731732
}
732733

734+
keyToLegacyUrlSafe(key: entity.Key, locationPrefix?: string): Promise<string>;
735+
keyToLegacyUrlSafe(
736+
key: entity.Key,
737+
callback: KeyToLegacyUrlSafeCallback
738+
): void;
739+
keyToLegacyUrlSafe(
740+
key: entity.Key,
741+
locationPrefix: string,
742+
callback: KeyToLegacyUrlSafeCallback
743+
): void;
733744
/**
734745
* Helper to create a URL safe key.
735746
*
@@ -744,26 +755,62 @@ class Datastore extends DatastoreRequest {
744755
* The location prefix of an App Engine project ID.
745756
* Often this value is 's~', but may also be 'e~', or other location prefixes
746757
* currently unknown.
747-
* @returns {string} base64 endocded urlsafe key.
758+
* @param {function} callback The callback function.
759+
* @param {?error} callback.err An error returned while making this request
760+
* @param {string} callback.urlSafeKey A Base64-encoded URL-safe key.
748761
*
749762
* @example
750763
* const {Datastore} = require('@google-cloud/datastore');
751764
* const datastore = new Datastore();
752765
* const key = datastore.key(['Company', 'Google']);
753766
*
754-
* datastore.keyToLegacyUrlsafe(key) //ag9ncmFzcy1jbHVtcC00NzlyEwsSB0NvbXBhbnkiBkdvb2dsZQw
767+
* datastore.keyToLegacyUrlSafe(key, (err, urlSafeKey) => {
768+
* if (err) {
769+
* // Error handling omitted.
770+
* }
771+
* console.log(urlSafeKey);
772+
* });
755773
*
756-
* @example
757-
* <caption>Create a complete url safe key using location prefix </caption>
758-
* const {Datastore} = require('@google-cloud/datastore');
759-
* const datastore = new Datastore();
760-
* const key = datastore.key(['Task', 123]);
774+
* //-
775+
* // Create a complete URL-safe key using a location prefix.
776+
* //-
761777
* const locationPrefix = 's~';
762778
*
763-
* datastore.keyToLegacyUrlsafe(key, locationPrefix) //ahFzfmdyYXNzLWNsdW1wLTQ3OXIKCxIEVGFzaxh7DA
779+
* datastore.keyToLegacyUrlSafe(key, locationPrefix, (err, urlSafeKey) => {
780+
* if (err) {
781+
* // Error handling omitted.
782+
* }
783+
* console.log(urlSafeKey);
784+
* });
785+
*
786+
* //-
787+
* // If the callback is omitted, we'll return a Promise.
788+
* //-
789+
* datastore.keyToLegacyUrlSafe(key).then((data) => {
790+
* const urlSafeKey = data[0];
791+
* console.log(urlSafeKey);
792+
* });
764793
*/
765-
keyToLegacyUrlsafe(key: entity.Key, locationPrefix?: string): string {
766-
return urlSafeKey.legacyEncode(this.projectId, key, locationPrefix);
794+
keyToLegacyUrlSafe(
795+
key: entity.Key,
796+
locationPrefixOrCallback?: string | KeyToLegacyUrlSafeCallback,
797+
callback?: KeyToLegacyUrlSafeCallback
798+
): Promise<string> | void {
799+
const locationPrefix =
800+
typeof locationPrefixOrCallback === 'string'
801+
? locationPrefixOrCallback
802+
: '';
803+
callback =
804+
typeof locationPrefixOrCallback === 'function'
805+
? locationPrefixOrCallback
806+
: callback;
807+
this.auth.getProjectId((err, projectId) => {
808+
if (err) {
809+
callback!(err);
810+
return;
811+
}
812+
callback!(null, urlSafeKey.legacyEncode(projectId!, key, locationPrefix));
813+
});
767814
}
768815

769816
/**
@@ -870,6 +917,27 @@ class Datastore extends DatastoreRequest {
870917
Transaction = Transaction;
871918
}
872919

920+
/*! Developer Documentation
921+
*
922+
* All async methods (except for streams) will return a Promise in the event
923+
* that a callback is omitted.
924+
*/
925+
promisifyAll(Datastore, {
926+
exclude: [
927+
'double',
928+
'isDouble',
929+
'geoPoint',
930+
'isGeoPoint',
931+
'int',
932+
'isInt',
933+
'createQuery',
934+
'key',
935+
'isKey',
936+
'keyFromLegacyUrlsafe',
937+
'transaction',
938+
],
939+
});
940+
873941
export {Datastore};
874942

875943
/**
@@ -932,3 +1000,7 @@ export interface DatastoreOptions extends GoogleAuthOptions {
9321000
apiEndpoint?: string;
9331001
sslCreds?: ChannelCredentials;
9341002
}
1003+
1004+
export interface KeyToLegacyUrlSafeCallback {
1005+
(err?: Error | null, urlSafeKey?: string): void;
1006+
}

test/index.ts

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -625,15 +625,49 @@ describe('Datastore', () => {
625625
});
626626
});
627627

628-
describe('keyToLegacyUrlsafe', () => {
629-
it('should convert key to url safe base64 string', () => {
628+
describe('keyToLegacyUrlSafe', () => {
629+
it('should convert key to URL-safe base64 string', () => {
630630
const key = new entity.Key({
631631
path: ['Task', 'Test'],
632632
});
633-
assert.strictEqual(
634-
datastore.keyToLegacyUrlsafe(key),
635-
'agpwcm9qZWN0LWlkcg4LEgRUYXNrIgRUZXN0DA'
636-
);
633+
const base64EndocdedUrlSafeKey = 'agpwcm9qZWN0LWlkcg4LEgRUYXNrIgRUZXN0DA';
634+
// tslint:disable-next-line: no-any
635+
(datastore.auth as any).getProjectId = (callback: Function) => {
636+
callback(null, 'project-id');
637+
};
638+
datastore.keyToLegacyUrlSafe(key, (err, urlSafeKey) => {
639+
assert.ifError(err);
640+
assert.strictEqual(urlSafeKey, base64EndocdedUrlSafeKey);
641+
});
642+
});
643+
644+
it('should convert key to URL-safe base64 string with location prefix', () => {
645+
const key = new entity.Key({
646+
path: ['Task', 'Test'],
647+
});
648+
const locationPrefix = 's~';
649+
const base64EndocdedUrlSafeKey =
650+
'agxzfnByb2plY3QtaWRyDgsSBFRhc2siBFRlc3QM';
651+
// tslint:disable-next-line: no-any
652+
(datastore.auth as any).getProjectId = (callback: Function) => {
653+
callback(null, 'project-id');
654+
};
655+
datastore.keyToLegacyUrlSafe(key, locationPrefix, (err, urlSafeKey) => {
656+
assert.ifError(err);
657+
assert.strictEqual(urlSafeKey, base64EndocdedUrlSafeKey);
658+
});
659+
});
660+
661+
it('should not return URL-safe key to user if auth.getProjectId errors', () => {
662+
const error = new Error('Error.');
663+
// tslint:disable-next-line: no-any
664+
(datastore.auth as any).getProjectId = (callback: Function) => {
665+
callback(error);
666+
};
667+
datastore.keyToLegacyUrlSafe({} as entity.Key, (err, urlSafeKey) => {
668+
assert.strictEqual(err, error);
669+
assert.strictEqual(urlSafeKey, undefined);
670+
});
637671
});
638672
});
639673

0 commit comments

Comments
 (0)